Unexpected Script Behaviour

Code

I’m on a blank baseplate and this is the code in a ServerScript:

game.Players.PlayerRemoving:Connect(function(Player)
	print("a")
	print(Player:GetRankInGroup(16616102))
end)

Here’s the output:

Problem

The player’s rank isn’t outputted and there’s no errors, however this only occurs when the player has been in-game for 60 seconds or longer.

Notes

  • Adding a game:BindToClose(function() didn’t fix it
  • Using GetRoleInGroup instead of GetRankInGroup didn’t fix it
2 Likes

You are calling a yielding function inside of a players.PlayerRemoving event so it’s likely the player is removed and thus the server is shut down before the call can complete.

3 Likes

I test it myself and it work so this is another problem i think.

3 Likes

The game probably shuts down before PlayerRemoving fires. You should add a delay before the game completely shuts down. Like this:

game.BindToClose:Connect(function() --Fires code here, closing the game after.
    task.wait(5) --Adds a 5 second delay.
end)

If any scripts use the BindToClose function, the game lets that code run, before it actually shuts down. So that code can just be a task.wait(5).

3 Likes

Btw I forgot to mention that the issue only happens after being in the game for 60 seconds or more.

Adding a game:BindToClose(function() fixes it so I added that to my main game, but now I’m getting Player:GetRankInGroup error: Player not in datamodel. Here’s my code:

Players.PlayerRemoving:Connect(function(Player)
	print(3)
	print(Player:GetRankInGroup(16616102))
	if Player:GetRankInGroup(16616102) >= 70 then
		print(3.5)
	end
end)

3 prints and so does my rank but then Player:GetRankInGroup error: Player not in datamodel prints and nothing prints after that.

3 Likes

“Player not in DataModel”

The Player has already left the game, you might want to extend the delay on the BindToClose function.

3 Likes

Updated it to 10 seconds and still getting the same error

2 Likes

Instead, you can actually wait for the data to finish, then close the game.

local finished = false

game.BindToClose:Connect(function()
    repeat task.wait(0.5) until finished == true
end)

Players.PlayerRemoving:Connect(function(Player)
	print(3)
	print(Player:GetRankInGroup(16616102))
	if Player:GetRankInGroup(16616102) >= 70 then
		print(3.5)
	end
        finished = true
end)

If you still get an error then it’s an issue with the data you’re getting from the group.

2 Likes

The same thing happens except I just get Not running script because past shutdown deadline too

2 Likes

Have you tried this in an actual game? Roblox Studio has some inconsistencies with how it runs compared to an actual Roblox server.

1 Like

Yes, this is how the issue was drawn to my attention in the first place

Fixed- the issue was that GetRoleInGroup and GetRankInGroup can’t be called when the player leaves the game. To counter this I had to store the player’s rank in a table when they join.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.