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

My BindToClose() isn't saving anything or anyone what am I doing wrong?

Ok there are no errors or anything in the output which means I guess the BindToClose isn't running

Basically I'm trying to save a IntValue named money and its inside the player Datastore is a Datastore called Datastore

Im trying to save everyones datastore incase of shut down

I tested this in game in a 1 player server because my game isn't popular

game:BindToClose(function()
	
	
for i,v in pairs(game.Players:GetPlayers()) do

succ,fail = pcall(function()
Cor = coroutine.wrap(function()
	DataStore:SetAsync(DataStore,v.UserId,v.Money)  
end)
Cor()
end)
if succ then
print('Success')
else print(fail)



end


	end
end)

1 comment

0

SetAsync is used incorrectly. It should only have two parameters, the key and the value. https://developer.roblox.com/api-reference/function/GlobalDataStore/SetAsync

2 answers

Hello! There are a couple problems with your code. One is the coroutine. There is no need for a coroutine there. Another problem, the main one, is the way your setting the async. SetAsync() takes to parameters, the key, and the value. ALSO, datastores only accept strings and numbers as data. So to make your code simpler and easier, just do the following:

game:BindToClose(function()
	for _,v in pairs(game.Players:GetChildren()) do
		Datastore:SetAsync(v.UserId, v.Money.Value) --Be aware this is if the Money Value is directly under the player, not under a folder in the player.
	end
end)

I hope this was able to help. With further questions/comments/concerns, feel free to reply to my answer! ~mc3334

I have a great suspicion that it's the coroutine in the function. BindToClose will only run once and then the server will close. Having the coroutine there would create a new thread but will shut down let the main thread continue on making the BindToClose function believe that no more code needs to run.

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