Unable to asign Badge upon player leaving

I’m creating a badge that is to be assigned to the player upon leaving the game, the issue is, is by the time they have left, the game can no longer assign the badge to them.

Error msg:

Any workarounds for this?

5 Likes

Did you assign the function through this line of code?

Players.PlayerRemoving:Connect("The function that gives badge")


-- if one player on the server
game:BindToClose("The function that gives badge")

If you did, then I dunno.

Hope this helps!

3 Likes

I used the first function you mentioned, not the second one. How do you use the second one?

2 Likes

yeah but how do I implement this

2 Likes

So, you should put this line of code at the end of script(to make sure it’s not gonna break anything), and if you have a name for your “give badge” function, just paste it in the brackets as shown here:
Screenshot_8

If it doesn’t work, try to copy the script on the screenshot and change this function:

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end

	for _, player in ipairs(Players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player) --this function, change it
		end)
	end
end

game:BindToClose(ServerShutdown)

I think that’s gonna work

1 Like

But this is for server shutdown correct? I’m looking at if just one player leaves the game independently.

2 Likes

Yes it is … Maybe a task.spawn would work. I would just give the badge at the log in.

2 Likes

You need all 2 functions be here, because it’s not gonna work if 1 player on the server and there is no server shutdown function, because this function will prevent server from shutting down too early to not initialize the first actual badge giving function

1 Like
game:BindToClose(function()
    for i, plr in pairs(game.Players:GetPlayers()) do
        -- Code to assign badge here
    end
end

The BindToClose method is called when the server shuts down. The server will shut down if the developer forces a shut down, or the last player leaves the server.

1 Like

I guess I’ll be the bearer of bad news. You cannot grant a badge to a player that is not in the server, as the warning message confirms. And since this badge is granted to players that leave the game, it’s just not possible to grant it immediately.

However, when a player leaves, you can save that the player should receive the badge, and you can grant it to them when they return (if they do)

BindToClose will not fix anything regarding the warning message. However, it will help if you want to save in a datastore that they should be granted the badge when they return. If the player to leave is the last player in the server, the server might shutdown before saving the fact they earned the badge.

Using BindToClose can keep the server open until all player’s data is saved.

you could use datastoreservice to check when the player leaves, and then rewarding the badge when the user rejoins at some point i guess