So this script is suppose to add values to a player, then using datastore, save and load their values. When I am the only player on the server, it will not save the value, I have a different script that changes the values to make sure it is saving or not, it is currently not, in play solo it works, but not a real server. Please help me.
ds=game:GetService("DataStoreService"):GetGlobalDataStore() hs=game:GetService("HttpService") local lastPlrStats={ } local lastPlrName="" local lastPlrId=nil local guy=nil local guystats=nil local lastPlrStats="" game.Players.PlayerAdded:connect(function(plr) local stats=Instance.new("IntValue") stats.Name="Stats" stats.Parent=plr --Values here for creating new stats for every player local starterchoosen=Instance.new("IntValue") starterchoosen.Name="StarterChoosen" starterchoosen.Parent=stats local firstpokemon=Instance.new("IntValue") firstpokemon.Name="FirstPokemon" firstpokemon.Parent=stats if ds:GetAsync(plr.userId.."Stats")~=nil then --Values here for loading stats already saved local userstats=hs:JSONDecode(ds:GetAsync(plr.userId.."Stats")) starterchoosen.Value=userstats[1] firstpokemon.Value=userstats[2] else --Values here for loading stats not saved (creating new stats for a player that hasn't played yet) starterchoosen.Value=0 firstpokemon.Value=0 end end) game.Players.PlayerRemoving:connect(function(plr) --Values here for saving stats (other than last person) ds:SetAsync(plr.userId.."Stats",hs:JSONEncode({ plr.Stats.StarterChoosen.Value; plr.Stats.FirstPokemon.Value; })) end) game.OnClose=function() print("Im dniog sothmineg") ds:SetAsync(lastPlrId.."Stats",lastPlrStats) print("saved") end while wait() do local players=game.Players:GetPlayers() if #players==1 then lastPlrName=players[1].Name lastPlrId=players[1].userId else lastPlrName="" lastPlrId=nil end if lastPlrId~=nil then guy=game.Players[lastPlrName] print("adding guy's stats") --Values here for the last player's stats to save guystats={ guy.Stats.StarterChoosen.Value; guy.Stats.FirstPokemon.Value; } lastPlrStats=hs:JSONEncode(guystats) end end
It's better to use .Changed on the leaderstats and not .PlayerRemoving