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

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

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)

2 comments

0

Sure I'll help, One moment.

0

Thanks ok!

1 answer

✓ Accepted answer

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)

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