Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Level Up System Script Loop Not Functioning?

Asked by
Aezion 2
5 years ago

Hi, I'm trying to make a leveling system server script into a game I'm building to work and I got it to where the stats and all will load onto the player but the trouble I'm having is I added a loop block for whenever player XP changes for the game to check if it's more or less than the XP needed to level up and if more to then check player level against maxlevel and if max level then nil but if below max level then level up however anytime I change my XP to higher than XP Needed no level up happens. I've tried writing it close to ten different ways now and honestly I'm relatively deficient in my LUA vocabulary so I apologize if it's some really simple error that I should've been aware of, if it is then I'll make sure to not let it happen again, but I kinda need someone to tell me my mistake before I can learn from my mistakes, so all help appreciated!

local DS        = game:GetService("DataStoreService")
local LevelSave = DS:GetDataStore("LevelSaveSystem")
local XPSave    = DS:GetDataStore("XPSaveSystem")
local GoldSave  = DS:GetDataStore("GoldSaveSystem")

game.Players.PlayerAdded:Connect(function(Player)
    local Folder   = Instance.new("Folder",Player)
    Folder.Name    = "leaderstats"

    local Level    = Instance.new("DoubleConstrainedValue",Folder)
    Level.Name     = "Level"
    Level.MaxValue = 100
    Level.MinValue = 1
    Level.Value    = LevelSave:GetAsync(Player.UserId) or 1
    Level.Changed:Connect(function()
        LevelSave:SetAsync(Player.UserId, Level.Value)
    end)

    local XP       = Instance.new("IntValue",Folder)
    XP.Name        = "XP"
    XP.Value       = XPSave:GetAsync(Player.UserId) or 0
    XP.Changed:Connect(function()
        XPSave:SetAsync(Player.UserId, XP.Value)
    end)

    local Gold     = Instance.new("IntValue",Folder)
    Gold.Name      = "Gold" 
    Gold.Value     = GoldSave:GetAsync(Player.UserId) or 0
    Gold.Changed:Connect(function()
        GoldSave:SetAsync(Player.UserId, Gold.Value)
    end)

    local XPN     = Instance.new("IntValue",Folder)
    XPN.Name      = "XPN"
    XPN.Value     = (math.floor((((8 * Level.Value) ^ 1.75) / 16) + 8.5))
    end)

while true do
    XP.Value.Changed:Connect(function()
        if XP.Value < XPN.Value or 0 then return end
        if XP.Value >= XPN.Value then
            if Level.Value < Level.MaxValue then
            local Humanoid = Player.Character:WaitForChild("Humanoid")

            XP.Value = XP.Value - XPN.Value
            Level.Value = Level.Value + 1
            Humanoid.MaxHealth = Humanoid.MaxHealth + 5
            Humanoid.Health = (Humanoid.MaxHealth * 0.90)
            end
            if Level.Value >= Level.MaxValue then return end
        end
    end)
end

game.Players.PlayerRemoving:Connect(function(Player)
    LevelSave:SetAsync(Player.UserId, Player.leaderstats.Level.Value)
    XPSave:SetAsync(Player.UserId, Player.leaderstats.XP.Value)
    GoldSave:SetAsync(Player.UserId, Player.leaderstats.Gold.Value)
end)


Sorry again if it ends up a dumb question :/

20:21:11.387 - ServerScriptService.Script:39: attempt to index global 'XP' (a nil value) 20:21:11.388 - Stack Begin 20:21:11.388 - Script 'ServerScriptService.Script', Line 39 20:21:11.389 - Stack End

0
Add a wait() to your loop, because you're making an inf loop which causes your studio to crash. B_rnz 171 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The "XP" variable is inside of your game.Players.PlayerAdded:Connect(function(Player) function so the XP variable will be undefined outside of that function. Also, you don't need a while true do to constantly check for the XP.Changed:Connect(function(), try putting your XP.Changed function inside of the game.Players.PlayerAdded:Connect(function(Player) part.

Also yeah, you can just do XP.Changed:Connect, you don't need to do XP.Value.Changed

Any other questions, add me on discord --> learning lua#6190

0
I hope you don't mind if I add you on disc just for a bit of just explanation cause while I get the gist of what you're saying I'm confused on some of the finer points Aezion 2 — 5y
Ad

Answer this question