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

Leaderstats not saving, help me please?

Asked by 6 years ago

Hello! I have an RPG in pre alpha. Recently, the levels and other stats stopped saving. Here's the script:

function onXPChanged(player, XP, level) 
if XP.Value>=level.Value * 50 then 
XP.Value = XP.Value - level.Value * 50 
level.Value = level.Value + 1 
player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50
player.Character.Humanoid.Health = player.Character.Humanoid.Health + 50
end 
end 

function onLevelUp(player, XP, level) 
if player.Character~=nil then 
for i = 1,5 do 
local fireworks = Instance.new("Part") 
fireworks.Shape = 0 
fireworks.formFactor = "Symmetric" 
fireworks.Size = Vector3.new(1,1,1) 
fireworks.BrickColor = BrickColor.Random() 
fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0) 
fireworks.Parent = game.Workspace 
game:GetService("Debris"):AddItem(fireworks, 2) 
fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30)) 
end 
end 
local m = Instance.new("Hint") 
m.Parent = game.Workspace 
m.Text = player.Name .. " has leveled up!" 
wait(5) 
m.Parent = nil 
end 

function saveScore4(player, score)
    player:SaveNumber("Stardust", score)
end

function saveScore(player, score)
    player:SaveNumber("Level", score)
end

function saveScore2(player, score)
    player:SaveNumber("XP", score)
end

function saveScore3(player, score)
    player:SaveNumber("Gold", score)
end


function loadScore(player, clickCounter)
    local score = player:LoadNumber("Level") 

    if score ~= 1 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore2(player, clickCounter)
    local score = player:LoadNumber("XP") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore3(player, clickCounter)
    local score = player:LoadNumber("Gold") 

    if score ~= 10 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function loadScore4(player, clickCounter)
    local score = player:LoadNumber("Stardust") 

    if score ~= 0 then
        clickCounter.Value = score
    else
        print("Nothing to load/score was 0")
    end

end

function onPlayerEntered(newPlayer)



        local stats = Instance.new("IntValue")
        stats.Name = "leaderstats"

        local clicks = Instance.new("IntValue")
        clicks.Name = "Level"
        clicks.Value = 1

        local clicks2 = Instance.new("IntValue")
        clicks2.Name = "XP"
        clicks2.Value = 0

        local clicks3 = Instance.new("IntValue")
        clicks3.Name = "Gold"
        clicks3.Value = 10

        local clicks4 = Instance.new("IntValue")
        clicks4.Name = "Stardust"
        clicks4.Value = 0


        clicks.Parent = stats
        clicks2.Parent = stats
        clicks3.Parent = stats
        clicks4.Parent = stats
        stats.Parent = newPlayer

clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end) 
clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) end) 

        newPlayer.WaitForDataReady = true

        -- try loading the player's score
        loadScore(newPlayer, clicks)
        loadScore2(newPlayer, clicks2)
        loadScore3(newPlayer, clicks3)
        loadScore4(newPlayer, clicks4)


newPlayer.CharacterAdded:connect(function(c) 
c.Humanoid.MaxHealth = newPlayer.leaderstats.Level.Value * 50 + 50
c.Humanoid.Health = newPlayer.leaderstats.Level.Value * 50 + 50 
end) 
end 

function onPlayerRemoving(player)
    print("Attempting to save score for " .. player.Name)
    local stats = player:FindFirstChild("leaderstats")
    if (stats ~= nil) then 
        local clicks = stats:FindFirstChild("Level")
        local clicks2 = stats:FindFirstChild("XP")
        local clicks3 = stats:FindFirstChild("Gold")
        local clicks3 = stats:FindFirstChild("Stardust")
        if (clicks ~= nil)and(clicks2 ~= nil)and(clicks3 ~= nil)and(clicks4 ~= nil) then
            saveScore(player, clicks.Value)
            saveScore2(player, clicks2.Value)
            saveScore3(player, clicks3.Value)
            saveScore4(player, clicks4.Value)
        end
    end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)

Any fixes for this error?

0
you would have better LUCK with DATA Stores TheScriptKing 102 — 6y
0
It worked fine before Code_Sensei 0 — 6y

Answer this question