Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Saving Script - Fails to save?

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

1

Accept my answer if it helped please!

0

What is the error, cause I knew there would be an error.

1 answer

✓ Accepted answer

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!

3 comments

1

Hi, Yeah, Thanks for your help :D

2

Only problem is that it still doesn't work ;-;

0

"It doesn't work" .. accepts answer

This discussion is preserved as it was. New answers, comments, and votes are closed.