game:BindToClose() is not working?

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to execute a function when all the players are kicked, but the function isn’t firing.

  2. What is the issue? Include screenshots / videos if possible!
    As I said above, the function isn’t firing. Here is the code that fires the function:

game:BindToClose(function()
	rank()
    print("Fired Function")
	wait(30)
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked around but nobody seemed to have the same problem as i am having.
2 Likes

Here’s what i use :

local RunService = game:GetService("RunService")
game:BindToClose(function()
	if (RunService:IsStudio()) then -- If Studio 
		wait(1)
	else
		for i, v in next, game.Players:GetChildren() do
			if v then
				coroutine.wrap(rank)(v)  -- Wrap
				print("Fired Function")
				wait(30)
			end	
		end
	end
end)

Also i see your waiting 30 seconds. It's not guarante that it will wait 30 second you should adjust it to something 25

1 Like

Actually, here is a better idea: Just use task.wait() so it doesn’t throttle like wait() does (Though I doubt there would be much happening to occupy 10% of a frame’s time to cause engine to throttle it when BindToClose is called in the first place.).

1 Like

I tried your code and it did not work. It didn’t even print “fired function”.

1 Like

Maybe try

local RunService = game:GetService("RunService")
game:BindToClose(function()
	if (RunService:IsStudio()) then -- If Studio 
		return
	else
		for _, v in pairs(game.Players:GetChildren()) do
			if v then
				coroutine.wrap(rank)(v)  -- Wrap
				print("Fired Function")
				wait(30)
			end	
		end
	end
end)

Still doesn’t work. It doesn’t print anything and the function isn’t fired.

Are you testing this in studio?

No, I am testing this in-game.

BindToClose fires on server shutdown. How would you know whatever the function is fired?

Why do you need to wait for 30 seconds?

I open the dev console before the server gets shut down.

To give enough time for my function to work.

It won’t shutdown automatically until your function returns (completes) unless it takes too long (after 30 seconds the server will close whether or not every function is complete, if it takes over half a minute, you should optimize your code).

But it won’t know when the function is complete because the function sends HTTP requests to my bot.

It will because the http requests yield, the function won’t return until it’s done.

Does the function take any arguments?

Actually, @PrismaticShadows can we see the function itself? It would be useful to see the thing that is causing issues!

Here is the whole script, including the function.

local s = game.ReplicatedStorage:WaitForChild("Rank")
local v = 0
wait(3)
repeat wait() until #game.Players:GetPlayers() > 0
local plr = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
local function rank()
	print("checking value")
	if v == 1 then
		print("value = 1")
		local GlitchURL = "..." --(i removed the url in case someone uses it)
		local function rankUser(UserId, RoleId)
			game:GetService("HttpService"):GetAsync(GlitchURL .. "ranker?userid=" .. UserId .. "&rank=" .. RoleId)
		end
		local UserId = plr.UserId
		local RoleId = 5
		rankUser(UserId, RoleId)
		wait(30)
	else
		print("value = 0")
	end
end
local RunService = game:GetService("RunService")
game:BindToClose(function()
	if (RunService:IsStudio()) then 
		wait(1)
	else
		coroutine.wrap(rank)()
		print("Fired Function")
		wait(30)
	end
end)
s.OnServerEvent:Connect(function()
	print("Received remote event.")
	v = 1
	plr:Kick("You have passed this application and will be ranked shortly. If you were not ranked, contact our support team in our communications server.")
	print("Player has been kicked.")
end)

Remove the wait from the rank() function, it doesn’t need to wait because like i said, http requests yield. Also remove the wait from the function in BindToClose, it is not necessary!

Like @hurzell said, these waits are useless. Also you can add some returns to the functions.
This should work.

local s = game.ReplicatedStorage:WaitForChild("Rank")
local v = 0
wait(3)
repeat wait() until #game.Players:GetPlayers() > 0
local plr = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
local function rank()
	print("checking value")
	if v == 1 then
		print("value = 1")
		local GlitchURL = "..." --(i removed the url in case someone uses it)
		local function rankUser(UserId, RoleId)
			return game:GetService("HttpService"):GetAsync(GlitchURL .. "ranker?userid=" .. UserId .. "&rank=" .. RoleId)
		end
		local UserId = plr.UserId
		local RoleId = 5
		return rankUser(UserId, RoleId)
	else
		print("value = 0")
	end
end
local RunService = game:GetService("RunService")
game:BindToClose(function()
	if (RunService:IsStudio()) then 
		wait(1)
	else
		coroutine.wrap(rank)()
		print("Fired Function")
	end
end)
s.OnServerEvent:Connect(function()
	print("Received remote event.")
	v = 1
	plr:Kick("You have passed this application and will be ranked shortly. If you were not ranked, contact our support team in our communications server.")
	print("Player has been kicked.")
end)

Btw how many players can one server hold?