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.
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:
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.
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
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.).
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)
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).
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)