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

How do I change this event to another? (Data Persistence)

I've got this much from the wiki, I need help with the last part.

scoreKey = "PlayerScore"

game.Players.PlayerAdded:connect(function(player)
	if player:WaitForDataReady() then
		local leaderstats = Instance.new("IntValue", player)
		leaderstats.Name = "leaderstats"
	
		local cash = Instance.new("IntValue", leaderstats)
		cash.Name = "Cash"
		cash.Value = player:LoadNumber(scoreKey)
	
		local xp = Instance.new("IntValue", leaderstats)
		xp.Name = "XP"
		xp.Value = player:LoadNumber(scoreKey)
	end
end)

game.Players.PlayerRemoving:connect(function(player) -- How do I change this so when you click a TextButton it will save your data?
	if player:findFirstChild("leaderstats") then
		player:SaveNumber(scoreKey, player.leaderstats.Score.Value)
	end
end)

1 answer

✓ Accepted answer

Make the script that you put this into a localscript (And put the localscript into the TextButton). Insert this into the script:

scoreKey = "PlayerScore"

game.Players.PlayerAdded:connect(function(player)
    if player:WaitForDataReady() then
        local leaderstats = Instance.new("IntValue", player)
        leaderstats.Name = "leaderstats"

        local cash = Instance.new("IntValue", leaderstats)
        cash.Name = "Cash"
        cash.Value = player:LoadNumber(scoreKey)

        local xp = Instance.new("IntValue", leaderstats)
        xp.Name = "XP"
        xp.Value = player:LoadNumber(scoreKey)
    end
end)

function Clicked()
local player=game.Player.LocalPlayer
    if player:findFirstChild("leaderstats") then
        player:SaveNumber(scoreKey, player.leaderstats.Score.Value)
    end
end)

script.Parent.MouseButton1Click:connect(Clicked)

It's the same thing, I just added a clicked event for the textbutton to save.

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