Server closing before thread finishes

So I’m using the PlayerRemoving function and in that function there is a HTTP post, an if statement, then another HTTP Post, the first Post request sends just fine, however the second one doesn’t fire. Both post requests work if one player is left in the server, however if you are the last player left in the server the second one doesn’t post. Does anyone know how to keep the server alive before the thread finishes?

2 Likes

Consider using game:BindToClose?

1 Like

To clarify what @SomeFedoraGuy meant, you’ll need to add this at the bottom of your script to force Studio to wait at least 2 seconds before closing the server:

if game:GetService("RunService"):IsStudio() then
	game:BindToClose(function()
		task.wait(2)
	end)
end

It doesn’t work I have no idea why, I have tried this before but the second post doesn’t occur unless there is someone else in the server…

We’ll need to see the script to be able to provide better suggestions, ideally using triple backsticks to format it like in this example:

```lua
print(“Hello, world!”)
```

game.Players.PlayerRemoving:Connect(function(plr)
	httpService:PostAsync("url", httpService:JSONEncode({Coins = plr.Coins.Value, Gems = plr.Gems.Value, UserId = plr.UserId}))
	local equippedPets = httpService:JSONDecode(plr.equippedPets.Value)
	if #httpService:JSONDecode(plr.equippedPets.Value) == 0 then
		equippedPets = {{UserId = plr.UserId}}
	end
	httpService:PostAsync("url", httpService:JSONEncode(equippedPets))
end)

Try this. There might be some syntax errors as on mobile


game:BindToClose(function()

for i,plr in (game.Players:GetPlayers()) do

	httpService:PostAsync("url", httpService:JSONEncode({Coins = plr.Coins.Value, Gems = plr.Gems.Value, UserId = plr.UserId}))
	local equippedPets = httpService:JSONDecode(plr.equippedPets.Value)
	if #httpService:JSONDecode(plr.equippedPets.Value) == 0 then
		equippedPets = {{UserId = plr.UserId}}
	end
	httpService:PostAsync("url", httpService:JSONEncode(equippedPets))
end
end)

Your making it only work if the player is in studio

That’s because the issue where the server closes too quickly only happens in Studio

On the Player servers have 30 seconds to run code before closing, so unless the function takes longer than 30 seconds to finish running it won’t be an issue

It doesn’t work on the player server either…

Pretty sure this won’t work because if the server is closing there won’t be any players in the server

Do any errors show up in the output when it fails to run?

It doesn’t seem to be showing any errors in the output it’s just not running

I rewrote the script you provided, the difference is subtle but I eliminated one JSONDecode call and made it so that the equippedPets is one table instead of 2:

game.Players.PlayerRemoving:Connect(function(plr)
	httpService:PostAsync("url", httpService:JSONEncode({
		Coins = plr.Coins.Value,
		Gems = plr.Gems.Value,
		UserId = plr.UserId
	}))

	local equippedPets = httpService:JSONDecode(plr.equippedPets.Value)

	if #equippedPets == 0 then
		equippedPets = {UserId = plr.UserId}
	end

	httpService:PostAsync("url", httpService:JSONEncode(equippedPets))
end)

Otherwise, nothing seems out of the ordinary so the issue you’re experiencing is quite unusual

Yeah, I don’t know what’s happening… Thanks for the help though! :slight_smile:

2 Likes