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

Saving Script - Fails to save?

Asked by
KordGamer 155
9 years ago

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.

1
Accept my answer if it helped please! EzraNehemiah_TF2 3552 — 9y
0
What is the error, cause I knew there would be an error. EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
5
Answered by 9 years ago

Use DataStores instead:


DataStores

DataStores is a way to save data, but It's better than Data Persistence. Link

local data = game:GetService("DataStores")
local save = data:GetDataStores("test")
save:GetAsync("Bob") --Load
save:SetAsync("Bob", 1337) --Save
--When the datastores isn't saved to anything, when you load it, it returns nil!


Final Product

local gold = game:GetService("DataStoreService"):GetDataStore("Gold") --Add I did was replace data persistence with datastores.
local lvl = game:GetService("DataStoreService"):GetDataStore("lvl")

function saveScore(player, score)
    gold:SetAsync(player.userId, score)
end

function saveLevel(player, score)
    lvl:SetAsync(player.userId, score)
end


function loadScore(player, killCounter)
    local score = gold:GetAsync(player.userId)
    if score ~= nil then
        killCounter.Value = score
    else
        killCounter.Value = 0
    gold:SetAsync(player.userId, 0)
    end

end

function loadLevel(player, killCounter)
    local score = lvl:GetAsync(player.userId)

    if score ~= nil then
        killCounter.Value = score
    else
        lvl:SetAsync(player.userId, 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")

        loadScore(newPlayer, clicks)
    loadLevel(newPlayer, clicks2)
        end

end

function onPlayerRemoving(player)
    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)



Hope it helps!

1
Hi, Yeah, Thanks for your help :D KordGamer 155 — 9y
2
Only problem is that it still doesn't work ;-; KordGamer 155 — 9y
0
"It doesn't work" .. *accepts answer* Goulstem 8144 — 9y
Ad

Answer this question