I'm writing a script to save the leaderstats of a player when they leave. There are no errors, yet it still doesn't save for some reason. I have tried re-writing the code and searching google, But I couldn't find anything. Could I have some help with this?
This is my script :
function saveScore(player, score)
player:SaveNumber("Gold", score)
end
function saveLevel(player, score2)
player:SaveNumber("Lvl", score2)
end
function loadScore(player, killCounter)
local score = player:LoadNumber("Gold")
if score ~= 0 then
killCounter.Value = score
else
print("Nothing to load/score was 0")
end
end
function loadLevel(player, killCounter2)
local score2 = player:LoadNumber("Lvl")
if score2 ~= 0 then
killCounter2.Value = score2
else
print("Nothing to load/score2 was 0")
end
end
function onPlayerEntered(newPlayer)
local stats = newPlayer:findFirstChild("leaderstats")
if (stats ~= nil) then
local clicks2 = stats:findFirstChild("Lvl")
local clicks = stats:findFirstChild("Gold")
newPlayer:WaitForDataReady()
loadScore(newPlayer, clicks)
loadLevel(newPlayer, clicks2)
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("Gold")
local clicks2 = stats:FindFirstChild("Lvl")
if (clicks ~= nil) then
saveScore(player, clicks.Value)
end
if (clicks2 ~= nil) then
saveLevel(player, clicks2.Value)
end
end
end
game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerRemoving)
I cannot figure this out. Any help is appreciated, Thank you if you could help me out!
I would just really like to know what I've done wrong.

2 comments
Accept my answer if it helped please!
What is the error, cause I knew there would be an error.