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

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

Asked by 10 years ago

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

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
10 years ago

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.

Ad

Answer this question