You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Studio求助:玩家触碰Checkpoint时Stage数值不增加

问题分析与修复方案

核心错误点

  • Stage初始值非法:把Stage.Value = nil改成Stage.Value = 0——IntValue的数值不能设为nil,否则后续tonumber(checkpoint.Name) == Stage.Value的判断永远不成立,直接卡死关卡升级逻辑。
  • 角色归属判断逻辑完全错误:char.Parent == plr.Character是错的,角色的父级是Workspace,而plr.Character就是角色实例本身。正确做法是用GetPlayerFromCharacter获取触碰角色对应的玩家,再验证是否为当前玩家。
  • WinSpawn触碰逻辑无玩家验证:原代码里WinSpawn的触发逻辑没判断触碰者是不是当前玩家,容易出现误触发问题。
  • Checkpoint命名需规范:所有关卡节点(除了WinSpawn和MainObbySpawn)的名称必须是纯数字字符串(比如"1""2"…"30"),不然tonumber(checkpoint.Name)会返回nil,无法匹配Stage数值。

修复后的完整代码

local Checkpoints = workspace.Checkpoints

game.Players.PlayerAdded:Connect(function(plr)
    -- 创建leaderstats及相关数值
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local Stage = Instance.new("IntValue")
    Stage.Name = "Stage"
    Stage.Value = 0 -- 初始值设为0,而非nil
    Stage.Parent = leaderstats

    local Wins = Instance.new("IntValue")
    Wins.Name = "Wins"
    Wins.Value = 0
    Wins.Parent = leaderstats

    local WinnerArea = Checkpoints:FindFirstChild("WinSpawn")
    if WinnerArea then
        WinnerArea.Touched:Connect(function(hit)
            local touchedPlr = game.Players:GetPlayerFromCharacter(hit.Parent)
            -- 验证触碰者是当前玩家且Stage达到30
            if touchedPlr == plr and Stage.Value == 30 then
                Wins.Value += 1
                Stage.Value = 0
            end
        end)
    end

    local lastCheckpointName = "MainObbySpawn" -- 初始默认出生点

    -- 遍历所有Checkpoint绑定触碰事件
    for _, checkpoint in pairs(Checkpoints:GetChildren()) do
        -- 跳过WinSpawn,避免误触发关卡逻辑
        if checkpoint.Name ~= "WinSpawn" then
            checkpoint.Touched:Connect(function(hit)
                local char = hit.Parent
                local touchedPlr = game.Players:GetPlayerFromCharacter(char)
                -- 验证触碰者是当前玩家且角色存在人形对象
                if touchedPlr == plr and char:FindFirstChild("Humanoid") then
                    local checkpointNum = tonumber(checkpoint.Name)
                    if checkpointNum then
                        -- 当前关卡等于Checkpoint编号时,升级关卡
                        if checkpointNum == Stage.Value then
                            Stage.Value += 1
                            lastCheckpointName = checkpoint.Name
                        end
                    elseif checkpoint.Name == "MainObbySpawn" then
                        -- 触碰出生点重置关卡
                        Stage.Value = 0
                        lastCheckpointName = checkpoint.Name
                    end
                end
            end)
        end
    end

    -- 角色重生时定位到最近 checkpoint
    plr.CharacterAdded:Connect(function(char)
        -- 等待角色关键部件加载完成
        char:WaitForChild("HumanoidRootPart")
        local checkpointToMoveTo = Checkpoints:FindFirstChild(lastCheckpointName)
        if checkpointToMoveTo then
            char:MoveTo(checkpointToMoveTo.Position)
        else
            --  fallback到MainObbySpawn
            local spawn = Checkpoints:FindFirstChild("MainObbySpawn")
            if spawn then
                char:MoveTo(spawn.Position)
            end
        end
    end)
end)

额外注意事项

  • 确保Workspace.Checkpoints下的所有关卡对象(除WinSpawn和MainObbySpawn)名称是连续数字字符串,且每个对象的CanTouch属性设为true。
  • WinSpawn对象要开启CanTouch,并且体积足够让玩家触碰触发。
  • 把这段代码放在ServerScriptService中运行,避免客户端脚本的权限问题。

内容的提问来源于stack exchange,提问作者Lazyy Mik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 09:40:30