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 my datastore save and load? ;-;

So I've got my datastore for onPlayerAdded which is

local DataStore = game:GetService('DataStoreService'):GetDataStore("StatStore")

game.Players.PlayerAdded:connect(function(player)
	
	local stats = Instance.new("IntValue",player)
	stats.Name = "Stats"
	local level = Instance.new("IntValue",stats)
	level.Name = "Level"
	local magic = Instance.new("IntValue",stats)
	magic.Name = "Magic"
	local stam = Instance.new("IntValue",stats)
	stam.Name = "Stamina"
	local cur = Instance.new("IntValue",stats)
	cur.Name= "Drake"
	local xp = Instance.new("IntValue",stats)
	xp.Name="EXP"
	xp.Value = 0

	xp.Changed:connect(function()
		if xp.Value>math.pow(1.5,player.Stats.Level.Value-1)*100 then
			xp.Value = 0
			player.Stats.Level.Value=player.Stats.Level.Value+1
			print("Level up")
		end
	end)

	local key = "player-"..player.UserId
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save format:  {level, magic, stam, xp, cur}
		level.Value = savedValues[1]
		magic.Value = savedValues[2]
		stam.Value = savedValues[3]
		xp.Value = savedValues[4]
		cur.Value = savedValues[5]
	else
		local ValuesToSave = {level.Value,magic.Value,stam.Value,xp.Value,cur.Value}
		DataStore:SetAsync(key,ValuesToSave)
	end
end) 

Then I got my save on playerLeaving script

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

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "player-"..player.UserId
	
	local ValuesToSave = {player.Level.Value, player.Magic.Value, player.Stamina.Value, player.EXP.Value, player.Drake.Value}
	DataStore:SetAsync(key, ValuesToSave)
end) 

I don;t understand as to why it wont save or load, I have tried different things for ValuesToSave

1 answer

You would need to put tostring before player.userId as player.userId goes between the parentheses of tostring as player.userId is an integer data type. And the u is lowercase in userId always.

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