Attempt to index nil with 'WaitForChild'

Ok so I am having a problem with my script

Script

while true do
		if game.Workspace:FindFirstChild(joinerName):WaitForChild("Head") ~= nil then --this line
			game.Workspace:FindFirstChild(joinerName):WaitForChild("Head").Party.Frame.Rank.Text = pName
			game.Players:WaitForChild(joinerName).PlayerGui.PartyGui.PartyGuiFrame.Transparency = 1
			game.Players:WaitForChild(joinerName).PlayerGui.PartyGui.PartyGuiFrame.UIStroke.Thickness = 0
			print(joinerName.Name.." Wants To Join Your Party ("..pName..")")
		end
		wait(.1)
	end

error

attempt to index nil with 'WaitForChild'
2 Likes

I’m pretty sure that means game.Workspace:FindFirstChild(joinerName) is returning as nil, and since :WaitForChild() isn’t a valid function of “nil”, it is giving an error.

1 Like

It’s nil (doesnt exists) and then WaitForChild is not a function of nil

1 Like

Is this same as workspace?

And, is this part of function for this?

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded

It might be easier to use game:GetService("Players"):FindFirstChild(joinerName) to find the player.

Maybe try doing

local plr = workspace:WaitForChild(joinerName)    
while true do
      if plr then
            plr:WaitForChild("Head").Party.Frame.Rank.Text = pName
            plr.PlayerGui.PartyGui.PartyGuiFrame.Transparency = 1
            plr.PlayerGui.PartyGui.PartyGuiFrame.UIStroke.Thickness = 0
            print(joinerName.Name.." Wants To Join Your Party ("..pName..")")
      end
      wait(.1)
 end

also, what’s the value of joinerName?

1 Like

Maybe get rid of the if statement and instead of saying, “game.Workspace:FindFirstChild(joinerName):WaitForChild(“Head”).Party.Frame.Rank.Text = pName”, replace the “game.Workspace:FindFirstChild(joinerName)” with game.Workspace:WaitForChild(joinerName).

Alright, thanks! Edited it.
Also, by if statement do you mean the second one?

Before I try to solve your problem where is the script located?

Alright, I’ve tried my best to make sense of the code. It’s difficult not knowing the use case but from what I can see of it, you’re using a loop to find a players’ characters’ head (the ‘joiner’) and setting some GUI properties.

The thing I’m confused about is, do we really want to be sending invites constantly? I’ve written (and commented for educational purposes) some code which may be wrong for your use case (please let me know!). But if it’s right, this should be the best way to do it.

The original answer to your issue is, you’re trying to :FindFirstChild() the character in the workspace, with no guarantee of the code actually finding it (so it could be nil). You’re then instantly using :WaitForChild() on that object. Well if it’s nil, :WaitForChild() won’t be a function of nil, because nil is equivalent of nothing/null, hence why the provided code isn’t working.

--[[ Using game.Players is unreliable, since Players can actually be renamed.
Using game:GetService(service: string) gets the object/instance directly. ]]
local PlayerService = game:GetService("Players")

--[[ I'm not sure of the context that this eventuality will trigger this functionality.
But based on your code, I'm guessing it's in some kind of loop, not sure where joinerName
and pName are coming from though. Will try my best to adapt. ]]

function loopJoinPartyOfPlayer(joinerName: string, playerName: string)
	while true do
		
		--[[ Try to get the Player object of the joiner using :WaitForChild().
		We can use the timeout parameter to ensure it doesn't get stuck infinitely here
		if the object can't be found. We can then check if there's no player 
		found, if not, we can't continue. ]]
		local joinerPlayer = PlayerService:WaitForChild(joinerName, 2)
		if not joinerPlayer then 
			continue 
		end

		-- Do the same as what we did for the joinerPlayer to ensure the PlayerGui exists.
		local joinerPlayerGui = joinerPlayer:WaitForChild("PlayerGui", 2)
		if not joinerPlayerGui then 
			continue 
		end

		--[[ Now we have the player object, we can just use joinerPlayer.Character to get the character.
		We'll :WaitForChild() on the joiners Head. If it exists, we can also assume the other objects 
		within it also exist. ]]
		local joinerHead = joinerPlayer.Character:WaitForChild("Head", 2)
		if not joinerHead then 
			continue 
		end

		--[[ These will error if in the unlikely event the objects don't exist, but this shouldn't 
		be the case, as we've verified the root objects exist (the head, the PlayerGui etc). ]]
		joinerHead.Party.Frame.Rank.Text = playerName 
		joinerPlayerGui.PartyGui.PartyGuiFrame.Transparency = 1

		-- Finally if everything works, you should see this print.
		print(joinerName.." Wants To Join Your Party ("..playerName..")")
		
		--[[ task.wait() is a wait type that works well with tasks, scheduling the current thread
		to resume after time has elapsed to prevent throttling. ]]
		task.wait(.1)
		
		--[[ Surely we don't want to constantly send invites though? So we could return here to
		to stop the process, thus breaking the loop. ]]
		return
	end
end

--[[ There's a neat thing called task.spawn(), which creates a new function on a seperate
thread. This will mean that any functionality below this will still work. We can also
set it to a variable so you can stop it/make it nil at any time you need to. ]]

--[[ Wherever you have this function defined, you'll be able to create a new task/thread which
runs without worrying to prevent code below to execute. Just make sure to have proper handling
on closing the thread once you're done with it, like so. ]]
local loopJoinTask = task.spawn(loopJoinPartyOfPlayer("xmthl", "LiljayGamer_X"))

-- And to stop it, you would do.
task.cancel(loopJoinTask)

My apologies that it’s a bit chunky, it’s all of the comments… :joy:

What I did forget to mention, it’s more reliable to get the character of a player by using the .Character property of the player, rather than finding an object in the workspace of the player’s name. There could be objects named the same as the player you’re trying to find, therefore it won’t get the right object or just outright not work.

I hope I helped. Let me know if you have any questions. :slight_smile:

1 Like