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

Why won't any of my stats save? They stick at 1 value and don't save.

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
local stats={"Intelligence","MaxEXP","EXP","MagicEnergy","MaxMagicEnergy","Level","LastLevel","Defense","Strength","Team","Cash","Wind","Fire","Earth","Water","Reputation"}
game.Players.PlayerAdded:connect(function(plyr)
	local a=Instance.new("NumberValue")
	a.Parent=plyr
	a.Name="leaderstats"
	for i=1,#stats do
		local stat=Instance.new("NumberValue")
		stat.Parent=a
		stat.Value=0
		stat.Name=stats[i]
	end
	local child=plyr.leaderstats:GetChildren()
	for i=1, #child do
		child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
	end
end)

game.Players.PlayerRemoving:connect(function(plyr)
	local child=plyr.leaderstats:GetChildren()
	for i=1, #child do
		child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
	end
end)

1 comment

0

Why are you setting the stat value when you are saving?

1 answer

✓ Accepted answer

You're trying to set child[i]'s value to a function that doesn't return a value on line 22... this doesn't make any sense. Just use the SetAsync function without trying to set the value of child[i].

Also, on line 15, you're setting the value when a player joins directly to the GetAsync function.. the GetAsync function can return nil, if this is the case then your code will error. This can happen if the player has never been to your game before, and they have no saved stats. So check if there are saved stats before setting them.

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
local stats={"Intelligence","MaxEXP","EXP","MagicEnergy","MaxMagicEnergy","Level","LastLevel","Defense","Strength","Team","Cash","Wind","Fire","Earth","Water","Reputation"}

game.Players.PlayerAdded:connect(function(plyr)
	local a=Instance.new("NumberValue",plyr)
	a.Name="leaderstats"
	for i=1,#stats do
		local stat=Instance.new("NumberValue",a)
		stat.Name=stats[i]
		local data = ds:GetAsync(plyr.userId..stats[i])
		if data then	
			stat.Value = data
		else
			stat.Value = 0
		end
	end
end)

game.Players.PlayerRemoving:connect(function(plyr)
	local child=plyr.leaderstats:GetChildren()
	for i=1, #child do
		ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
	end
end)

1 comment

0

OH, thanks that made it easier to understand.

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