game:BindToClose() does not run consistently

just yesterday this was all working fine and running every time but now it just doesnt work now

what i have is a server creation system where servers are created on a press of a button. their data is stored within a datastore. then they are tped to that server. when the server closes (when everybody leaves) i use game:BindToClose() to run a clear data function.

function clearData() -- binded method
	if owner.serverId == nil or owner.player == nil then
		return
	end

	MessagingService:PublishAsync("ServerConnections", {
		serverId = owner.serverId,
		owner = owner.player.UserId,
		close = true,
	})
	destroyServerData(owner.serverId)
end

function destroyServerData(id)
	local data = ServerStore:GetAsync(id)
	if data == nil then
		return "not a server"
	end

	ServerStore:RemoveAsync(id)
end

(no owner.serverid and owner.player are not nil. i know that for sure)
(also data isnt nil either because after using the datastore editor i see it right there)
(ServerStore is a custom module i made to configure a datastore method im using)

i use messaging service to send back to the lobby server to remove the data it stores within its cache table (since i dont request to the datastore every frame, thats not smart) everything worked yesterday. but the day before it wasnt working and now today it is. so game:BindToClose() isnt running consistently. its very hard to debug this since i cant view the dev console after the server closes.
testing this in studio wont work either since i need the data from the lobby where i was teleported from. using webhooks to send to discord wont work either since it will take too long to send and roblox shuts down the server after 30 seconds no matter what and im pretty sure the bind to close function isnt even running

so what im asking is there any other method that can consistently clear data on server close. this needs to count for all the players leaving, the server shutting down, and a possible server crash. or am i just using game:BindToClose() incorrectly

Could you also send the code where you do bind to close?

Random guess here, but it might be that the messaging service doesn’t constantly send the message after the server is gone. Have you tried delaying the server shutdown a few seconds by yielding the function bound to close?

I’m not familiar with this edge case, but I do know that BindToClose runs super consistently as long as it’s not a server crash ending the server.

it doesnt matter if messaging service sends to the other server or not since thats just for lobby servers that are still online which wont be often (when trying to join the server i check if the data is still in the datastore before tping)

this is all i do to bind it

if not RunService:IsStudio() then
    game:BindToClose(clearData)
end

i have not tried yielding the function but i cannot for a while so ill send another message when i can try that

update:
so turns out owner.player was nil sometimes it just depended on how fast roblox was running at that time

also for some reason publishAsync would sometimes error out the funciton causing the removal of the data in the datastore to never run so i moved it above it
and i also added task.wait(1) and i believe everything is working now

new function

function clearData()
	if owner.serverId == nil or owner.userid == nil then
		return
	end
	task.wait(1)
	
	destroyServerData(owner.serverId)
	MessagingService:PublishAsync("ServerConnections", {
		serverId = owner.serverId,
		owner = owner.userid,
		close = true,
	})
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.