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

Roblox自制奖励系统XP无法与等级进度条同步故障求助

问题修复方案

核心故障点梳理

  • 奖励发放逻辑错误:未直接修改经验值,反而错误绑定了不存在的事件,同时经验值路径定位错误
  • UI代码事件参数错误:level.Changed 回调参数覆盖了原level实例变量,导致读取属性报错
  • leaderstats代码语法缺失:末尾缺少闭合的end语句,导致升级逻辑无法正常运行

修复后的各模块代码

1. 奖励发放程序(服务端脚本,挂在NPC下)

local Humanoid = script.Parent.Humanoid
local Experience = 10
function Dead()
    local tag = Humanoid:FindFirstChild("creator")
    if tag ~= nil and tag.Value ~= nil then
        local player = tag.Value
        local leaderstats = player:FindFirstChild("leaderstats")
        if leaderstats ~= nil then
            -- 发放金币
            leaderstats.Cash.Value = leaderstats.Cash.Value +50
            -- 直接发放经验:经验存放在player.Level.Current下,不是leaderstats下
            local currentExp = player.Level.Current
            currentExp.Value += Experience
            wait(0.1)
            script:Remove()
        end
    end
end

Humanoid.Died:Connect(Dead)

原代码错误点:错误调用Connect绑定不存在的事件,完全不会触发经验增加逻辑,同时经验值路径定位错误

2. 经验条UI程序(客户端脚本,挂在UI下)

--Player related variables--
local player = game.Players.LocalPlayer
local level = player:WaitForChild("Level")
local current = level:WaitForChild("Current")
local max = level:WaitForChild("Max")

--UI related variables--
local gui = script.Parent
local exterior = gui:WaitForChild("Exterior")
local label = exterior:WaitForChild("Label")
local exp = exterior:WaitForChild("Exp")
local bar = exterior:WaitForChild("Bar")

--Change stats upon join--
label.Text = "Level "..level.Value
exp.Text = current.Value.."/"..max.Value.." Exp"
bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)

level.Changed:Connect(function(newLevelVal)
    -- 原错误点:第二个参数覆盖了level实例,直接用外部的level变量即可
    label.Text = "Level "..newLevelVal
    exp.Text = current.Value.."/"..max.Value.." Exp"
    bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
end)

current.Changed:Connect(function(val)
    exp.Text = current.Value.."/"..max.Value.." Exp"
    bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
end)

原代码错误点:level.Changed回调多写了第二个参数,导致level变量被覆盖为数字,调用level.Value触发报错,UI无法更新

3. leaderstats服务端程序(放在ServerScriptService下)

local DataStore = game:GetService("DataStoreService"):GetDataStore("butthole")

game.Players.PlayerAdded:Connect(function(player)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = player
    
    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Value = 900
    cash.Parent = folder
    --start
    local level = Instance.new("IntValue", player)
    level.Name = "Level"
    level.Value = 1
    
    local exp = Instance.new("IntValue", level)
    exp.Name = "Current"
    exp.Value = 0
    
    local maxExp = Instance.new("IntValue", level)
    maxExp.Name = "Max"
    maxExp.Value = 100

    exp.Changed:Connect(function(val)
        if exp.Value >= maxExp.Value then
            level.Value = level.Value + 1
            exp.Value = 0
            maxExp.Value = math.floor(maxExp.Value * 2.5) -- 建议取整避免小数
        end
    end) -- 原代码缺少这个end
end) -- 原代码缺少这个end

原代码错误点:末尾缺少两个闭合end,语法错误导致升级逻辑完全不生效,同时建议最大经验取整避免出现小数

内容的提问来源于stack exchange,提问作者Keith Jacobs

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 20:06:06