Roblox RPG游戏快速升级系统

大家好,这几乎是我第一次编写脚本,我正在尝试修复一个已有的升级系统。这是一个相当快速的升级系统,问题在于如果玩家的经验值远远超过升级所需的经验值,我该如何立即使其升级,就像这个例子一样:

我有1000万经验,我只需要10000经验就可以升到100k。我该如何让升级立即发生?

由于我不想让玩家在游戏中遇到经验问题,请帮忙解决。

local LevelUp = function(plr, Level, XP)
    if XP.Value >= Level.Value * 25 then
        -- Leveling
        XP.Value = XP.Value - Level.Value * 25
        Level.Value = Level.Value + 1
        -- Health
        if (not plr.Character) then return end
        local hum = plr.Character:WaitForChild("Humanoid")
        hum.MaxHealth = hum.MaxHealth + 10
    end
end

game.Players.PlayerAdded:Connect(function(plr)

    plr.CharacterAdded:Connect(function(chr)
        local hum = chr:WaitForChild("Humanoid",10)
        local leaderstats = plr:WaitForChild("leaderstats")
        local Level = leaderstats:WaitForChild("Level")
        repeat wait() until plr:FindFirstChild("DataLoaded")~=nil
        hum.MaxHealth = Level.Value * 10 + 100
        hum.Health = hum.MaxHealth
    end)

    local leaderstats = plr:WaitForChild("leaderstats")
    local Level = leaderstats:WaitForChild("Level")
    local XP = leaderstats:WaitForChild("XP")

    Level.Changed:Connect(function() wait() LevelUp(plr, Level, XP) end)
    XP.Changed:Connect(function() wait() LevelUp(plr, Level, XP) end)
end)

这是脚本的样子。我应该添加什么来能够“跳过”多余的经验,使玩家缓慢升级?

这就是目前等级的形象。我已经静止了一个多小时了(Td=1+e42)

原文链接 https://stackoverflow.com/questions/70355120

点赞
stackoverflow用户2860267
stackoverflow用户2860267

一些经验值块被获取后,可以使用 while 循环工具。

当玩家获得一块经验值块后,调用 LevelUp 函数。然后,只要玩家拥有足够的经验值,就提高玩家的等级。while 循环将一直执行此循环,直到他们没有足够的经验值来继续升级。

local function LevelUp(plr, Level, XP)
    -- 找到玩家人形
    local hum = nil
    if (plr.Character) then
        hum = plr.Character:WaitForChild("Humanoid")
    end

    local currentLevel = Level.Value
    local currentExp = XP.Value
    local targetExp = Level.Value * 25
    while (curentExp >= targetExp) do
        -- 升级
        currentExp -= targetExp
        currentLevel += 1

        -- 重新计算下一级所需经验值
        targetExp = currentLevel * 25

        -- 增加玩家的健康值
        if (hum) then
            hum.MaxHealth = hum.MaxHealth + 10
        end
    end

    -- 在最后更新 NumberValues,以免触发无限 while 循环
   if (currentLevel ~= Level.Value) then
       Level.Value = currentLevel
   end
   if (currentExp ~= XP.Value) then
       XP.Value = currentExp
   end
end
2021-12-14 22:50:32