Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script throws error whenever a player leaves?

Hi I’ve made a script which will put an image above users who are in a certain group and it all works properly.

The problem is that I keep getting an error on lines 14 and 35 whenever a player leaves which I believe is because the script is no longer able to find them.
My question is how am I able to fix this error?
I attempted to remove the character check and replace it with a wait so when the respawn is called the script will be able to find them once generated but this stops the script from working entirely.

Here is the code:

   local groupID = 299014
   local GroupService = game:GetService("GroupService")
   local Players = game:GetService("Players")
   local function onCharacterAdded(player)
   
   local groups = GroupService:GetGroupsAsync(player.UserId)
   
   for _, groupInfo in pairs(groups) do
   wait(1)
   local groupno = groupInfo.Id
   if groupInfo then
   if groupno == groupID then
   local gui=Instance.new("BillboardGui")
   gui.Parent=player.Character.Head
   gui.Adornee=player.Character.Head
   gui.Size=UDim2.new(3,0,3,0)
   gui.StudsOffset=Vector3.new(0.5,2,0)
   local text=Instance.new("ImageLabel")
   text.Image = "rbxgameasset://Images/Photo"
   text.Size=UDim2.new(1,0,1,0)
   text.Position=UDim2.new(-0.225,0,-0.35,-45)
   text.BackgroundTransparency = 1
   text.Parent=gui
   print(player.Name.. " is a member of the group")
   end
   end
   end
   end
   
   Players.PlayerAdded:Connect(onCharacterAdded)
   
   local function onPlayerRespawn(character)
   character.Changed:Connect(function(property)
   if (property == "Character") then
   onCharacterAdded(character)
   print("Reloading " ..character.Name.. "'s GUI.")
   end
   end)
   end
   Players.PlayerAdded:Connect(onPlayerRespawn)

2 answers

✓ Accepted answer

Make a table of the players currently in game and connect a PlayerRemoving event:

local players = {}
game.Players.PlayerAdded:Connect(function(playerWhoJoined)
    table.insert(players, playerWhoJoined)
end)
game.Players.PlayerRemoving:Connect(function(playerLeaving)
    table.remove(players, playerLeaving)
end)

What this does is that it stores a table of the players who are currently in game. When a player leaves, that player is removed from the table. If you include this in your code it should fix your problem (as long as you adjust your code to use the table).

Add a :FindFirstChild / :WaitForChild(obj, timeout) check to make sure that the player is valid, and that the character's body is not nil. I assume the error was attempting to index a nil value.

FindFirstChild checks are your best friend when you're unsure if something is nil or not.

WaitForChild checks are your best friend if you know that the object is there but it's still erroring for some unknown reason.

This discussion is preserved as it was. New answers, comments, and votes are closed.