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)
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)