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

Can I have a little help with saving and loading? [UNSLOVED]

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

2 comments

0

Where are you having trouble, any specific points. What about output, is there any?

0

Output doesn't say anything, the trouble is with saving the last player's stats.

1 answer

It's better to use .Changed on the leaderstats and not .PlayerRemoving

2 comments

0

The comments are made for a reason. If you aren't going to help the asker, there is no need to answer.

0

But then, everytime the player changes it will save, causing the datstore access to go over the limit.

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