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

Why won't this code work / save?

This is supposed to be a DataStore script I did from a tutorial but it doesn't seem to work. Any idea what's wrong?

Here's part one of the script:

local DataStore = game:GetService("DataStoreService"):GetDataStore("LolWhyDa")


game.Players.PlayerAdded:connect(function(player)
	
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", stats)
	points.Name = "Points"
	
	local coins = Instance.new("IntValue", stats)
	coins.Name = "Coins"
	
	local key = "user-"..player.userId
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save Format: {points, coins}
		points.Value = savedValues.Value[1]
		coins.Value = savedValues.Value[2]
	else
		local valuesToSave = {points.Value, coins.Value}
		DataStore:SetAsync(key, valuesToSave)
		
	end
end)

Part 2, located in a seperate script

local DataStore = game:GetService("DataStoreService"):GetDataStore("LolWhyDa")


game.Players.PlayerRemoving:connect(function(player)
	
	local key = "user-"..player.userId
	--Save key: {points, coins}
	local valuesToSave = {player.leaderstats.Points.Value, player.leaderstats.Coins.Value}
	DataStore:SetAsync(key, valuesToSave)

end)

both scripts are in ServerScriptService.

2 comments

0

leaderstats must be a model

0

I did that, still didn't save!

2 answers

With data stores it is dificult, but as johndeer said, that is the best place to start :/

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