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⌠
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. 