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

How would I rewrite this to use DataStore instead of DataPersistence? Help needed!

Asked by
CHATED 10
10 years ago

I'm very confused with DataStore and I'd like to know how I would rewrite this code using DataStore instead of DataPersistence.

function SaveScore(plyr, score)
    plyr:SaveNumber("stage", score)
end




function onClicked()

    player = script.Parent.Parent.Parent.Parent.Parent
        player:WaitForDataReady()
if player.DataReady == true then
SaveScore(player, player.leaderstats:findFirstChild(script.Parent.Parent.Parent.Configuration.Type.Value).Value)
m = Instance.new("Hint",player.PlayerGui)
m.Text = script.Parent.Parent.Parent.Configuration.Type.Value.. " saved!"
game:service("Debris"):AddItem(m, 3)
else
m = Instance.new("Hint",player.PlayerGui)
m.Text = "Wait until DataReady!"
game:service("Debris"):AddItem(m, 3)

end
end 

script.Parent.MouseButton1Click:connect(onClicked)
0
Sure I'll help, One moment. xImmortalChaos 565 — 10y
0
Thanks ok! CHATED 10 — 10y

1 answer

Log in to vote
1
Answered by 10 years ago

local DataStore = game:GetService("DataStoreService"):GetDataStore("Store1") --You can use GetDataStore for just regular games, and GetGlobalDataStore for Universes.

game.Players.PlayerAdded:connect(function(Player) --Called when a player joins the game.
repeat wait() until Player:FindFirstChild("leaderstats") --Assuming you have another script that creates this.
local Data = DataStore:GetAsync("Data_"..Player.userId) --Location of specific data, Thus being Player data.
if Data then --Checks to see if the player already has saved data.
Player.leaderstats.Stage.Value = Data
end
end)

game.Players.PlayerRemoving:connect(function(Player) --Called when a player is leaving the game.
local Data = SetAsync("Data_"..Player.userId)
if Player:FindFirstChild("leaderstats") then --Looks for leaderstats.
Data = Player.leaderstats.Stage.Value --Sets the player's leaderstat stage to the DataStore.
end
end)
Ad

Answer this question