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

How would I save it to player?

I'm using DataStore.How would I make the value save to the player instead of server.Note: The Value is in the player and not a variable.

2 answers

Actually, Data Store should be rather used to store player save data as it says in the Data Persistence Wiki itself. tldr; don't use Data Persistence for anything. (Except if you're saving Instances, like models and parts)

Data Persistence is a method of saving data for games so that the data can then be loaded when the player visits the game again. Data Stores were more recently introduced and are more robust than Data Persistence and are now the recommended method of storing player data.

This is something I wrote up in a few minutes, so forgive of the rustiness. Just tell me if there is anything wrong because I haven't tested it. xD

local ds = game:GetService("DataStoreService"):GetDataStore("Score")
 
game.Players.PlayerAdded:connect(function(player)
	local key = "user_" .. player.userId
	local score = 0
	ds:UpdateAsync(key, function(oldValue)
		local newValue = oldValue or 0 --oldValue might be nil
		score = newValue
		return newValue
	end)
	repeat wait() until player.Character
	local stat = Instance.new("IntValue")
	stat.Name = "leaderstats"
	local ls = Instance.new("Score")
	ls.Value = score
	ls.Parent = stat

	stat.Parent = player
		
end)

game.Players.PlayerRemoving:connect(function(player)
	local key = "user_" .. player.userId
	ds:UpdateAsync(key, function(oldValue)
		local newValue = player.leaderstats["Score"].Value
		return newValue
	end)
end)

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