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

DataStore loads string value as 0?

I am making a script which saves this custom shirts name in a string value. Changing this value in the game (Server side) works fine. When I join the game, the game loads the shirts value (String) as the string "0". Why is this?

Here is a shortened version of how I added the value into the player:

game.Players.PlayerAdded:Connect(function(p)
	local datafolder = Instance.new("Folder",p)
	datafolder.Name = "Data"
	local shirt = Instance.new("StringValue",datafolder)
	shirt.Name = "Shirt"
	shirt.Value = "None"
end)

Here is the entire script I use to save my data:

local DSService = game:GetService('DataStoreService'):GetDataStore('Data')

game.Players.PlayerAdded:connect(function(plr)
wait(0.5)
	local uniquekey = 'id-'..plr.userId
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		plr.Data.SkinColor.Value = GetSaved[1]
		plr.Data.HipObject.Value = GetSaved[2]
		plr.Data.Hat.Value = GetSaved[3]
		plr.Data.BackObject.Value = GetSaved[4]
		plr.Data.RokushikiLearnt.Value = GetSaved[5]
		plr.Data.HakiLearnt.Value = GetSaved[6]
		plr.Data.HakiLevel.Value = GetSaved[7]
		plr.Data.MaxStamina.Value = GetSaved[8]
		plr.Data.Endurance.Value = GetSaved[9]
		plr.Data.Shirt.Value = GetSaved[10]
	else
		local NumbersForSaving = {plr.Data.SkinColor.Value, plr.Data.HipObject.Value, plr.Data.Hat.Value, plr.Data.BackObject.Value, plr.Data.HakiLearnt.Value, plr.Data.RokushikiLearnt.Value, plr.Data.HakiLevel.Value,plr.Data.Endurance.Value,plr.Data.MaxStamina.Value,plr.Data.BlackLegLearnt.Value,plr.Data.RyusokenLearnt.Value,plr.Data.ElectroLearnt.Value,plr.Data.Shirt.Value}
		DSService:SetAsync(uniquekey, NumbersForSaving)
	end		
end)						
							
game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.Data.SkinColor.Value, plr.Data.HipObject.Value, plr.Data.Hat.Value, plr.Data.BackObject.Value, plr.Data.HakiLearnt.Value, plr.Data.RokushikiLearnt.Value, plr.Data.HakiLevel.Value,plr.Data.Endurance.Value,plr.Data.MaxStamina.Value,plr.Data.BlackLegLearnt.Value,plr.Data.RyusokenLearnt.Value,plr.Data.ElectroLearnt.Value,plr.Data.Shirt.Value}
	DSService:SetAsync(uniquekey, Savetable)				
end)

I am also guessing that my data saving script is really bad. I don't know any better ways to write one. I need some help with that too.

1 answer

I've answered this same kind of question 2 days ago aswell. The problem with these kind of scripts that save data when the user leaves is, when the last person leaves (which is the case in studio) the server immediately exits and stops your request to save data.

You can use game:BindToClose(), iterate all the remaining players and save the values, but also use PlayerRemoving too.

2 comments

0

Do you place this game:BindToClose() at the start of any script? Or do I use it as a function somewhere?

1

game:BindToClose(function() saving code end) if I remember correctly

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