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

Why is the Spray paint tool not saved?

I have three items (tools) to save and load. They are the BloxyCola, TeddyBear and the Spray. An item will be added to the player's StarterGear and Backpack if it is bought.

I want the game to save what is added to the StarterGear, then give them what they bought when they join back in.

I bought all three items . Unfortunately, when I tested it and joined the game again, the game didn't gave me the Spray! I tried a few more times, but it didn't help at all!

No errors have been stated in the output!

Here is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("PlayerItems")

game.Players.PlayerAdded:connect(function(plr)
	local key = "Player-"..plr.UserId
	local getSaved = ds:GetAsync(key)
	
	if getSaved then
		print(table.concat(getSaved, ", ")) -- The output only printed the TeddyBear and BloxyCola.
		for _, item in pairs(getSaved) do
			game.ReplicatedStorage.Items:WaitForChild(item):Clone().Parent = plr:WaitForChild("StarterGear")
			game.ReplicatedStorage.Items:WaitForChild(item):Clone().Parent = plr:WaitForChild("Backpack")
		end
	else
		local items = {}
		ds:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(plr) -- I think there is something wrong in here... I'm not sure.
	local key = "Player-"..plr.UserId
	local items = {}
	for _, gear in pairs(plr:WaitForChild("StarterGear"):GetChildren()) do
		table.insert(items, gear.Name)
	end
	
	print(table.concat(items, ", ")) --Another reference... I tried in Studio and it printed all three items.
	
	ds:UpdateAsync(key, items)
	print("Player Item Save Success") 
	wait(2)
end)

I hope this would be enough to understand. Any help would be appreciated! :D

2 comments

0

Why are you using update async?

0

well i haven't known a lot about DataStores just yet. I hope someone will tell me the DIFFERENCE between SetAsync and UpdateAsync

1 answer

✓ Accepted answer

You're using DS:UpdateAsync in a place when you should be using DS:SetAsync. Since you want to know the difference between the two, here it is:

UpdateAsync expects a function as the second argument, and it calls the function with the current value of the function. UpdateAsynccalls that function with the current value of the key, and that function MUST return the new value. It's useful for if you have multiple scripts or servers updating the same value.

SetAsync just expects the new value as the second argument, and if you have multiple scripts/servers updating the same value, they could overwrite something. Here's an example:

If you were giving players cash for enemy kills, and you did it like this: DS:SetAsync(key.."_cash", DS:GetAsync(key.."_cash")+100), two enemies could get the same value because of caching and the player loses out on some cash. So the valid solution to that problem is using UpdateAsync:

DS:UpdateAsync(key.."_cash", function(oldCash)
	return oldCash+100
end)

1 comment

0

Thanks for the explaning! :D

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