Studio not responding whenever leaving after a server script errors

Every time I get an error on a server script and then stop the current game, studio hangs until the shutdown deadline. This happens across all my games and this does not happen if the error is from a local script. It could be the :BindToClose() function, but I am not sure.

:BindToClose() function on server

game:BindToClose(function()
	if studio then task.wait(2) if studioleave then return end end -- this just waits 2 seconds to see if PlayerRemoving has already saved the player's data. tried with wait(2) but no difference

	for _, plr in ps:GetPlayers() do
		if not sessiondata[plr.UserId] then continue end
		setdata(plr.UserId)
	end
end)

I have used the print to see what happens, it stops at the wait function (both task.wait and normal wait) and doesn’t do anything after that. If the problem is to do with waiting, then is there an alternative to checking if PlayerLeaving has already saved data or if I should just make it save twice.