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

Why does this UpdateAsync not work and doesn't even print out any error?

I've be trying to get get this chunk of code to save the players coins when they leave; I don't mean to use SetAsync either. The first two print (the CoinValue and the string inside the pcall) prints, but the prints at the bottom doesn't print (the if statements nor the print under the end). Needless to say, the CoinValue isn't being updated. And I've also checked that newValue isn't nil from the print(CoinValue) and it is a number.

Players.PlayerRemoving:Connect(function(player)
	local key = tostring(player.UserId)
	local CoinValue = PlayerCoins:FindFirstChild(key).Value
	local CoinStore = DataStoreService:GetDataStore("CoinStore")
	print(CoinValue)

	local success, Result = pcall(function()
		print("running function")
		CoinStore:UpdateAsync(key,function(oldVal)
			local newVal = CoinValue
			if newVal ~= nil then
				return newVal
			else
				return oldVal
			end
		end)
	end)
	
	print(success)
	
	if success then
		print("yay")
		print(success)
		print(Result)
	else 
		print("nope it failed")
	end
end)

1 answer

Figured it out. Due to this being ran in Studio, it timed out before the full script was ran. Solved by adding a BindToClose function with a wait(3) in it.

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