HumanoidRootPart is nil, event after waiting

I’ve tried so many different ways to insert particles into a humanoid root part when the character spawns. I use a remote event because I have settings for people who do not want to see other people’s particles. Usually, it will get the root part when the player first joins and then assign nil after every respawn, but sometimes it will just assign nil when the player joins too. I’ve tried regular indexing, WaitForChild, and even a bindable event. Here’s what I have so far, and it’s not working as intended.

--Server
plr.CharacterAdded:Connect(function(char)
	if char.HumanoidRootPart == nil then		
             char.ChildAdded:Connect(function(child)
		if child.Name =="HumanoidRootPart" then
			rStorage.MiscEvents.RootPartAdded:Fire()
		end
	end)
	rStorage.MiscEvents.RootPartAdded.Event:Wait()
       end
	for i, v in pairs(pService:GetPlayers()) do
		if v:WaitForChild("SettingsFolder").ShowParticles.Value then
rStorage.MiscEvents.AddParticle:FireClient(v,particlesMod[plr.SettingsFolder.Equipped.Value],char.HumanoidRootPart)
		end
	end
end)
--Client
rStorage.MiscEvents.AddParticle.OnClientEvent:Connect(function(pInfoTable,rootpart)
	print('client function firing', tostring(rootpart))
--Output: client function firing nil

Sorry that the code is unreadable, it doesn’t formal well from studio

Couldn’t you just reference a variable by using some scopes?

--Server
plr.CharacterAdded:Connect(function(char)
    local RootPart = nil

	if char:FindFirstChild("HumanoidRootPart") == nil then		
             char.ChildAdded:Connect(function(child)
		if child.Name == "HumanoidRootPart" then
            RootPart = child
			rStorage.MiscEvents.RootPartAdded:Fire()
		end
    else
        RootPart = char.HumanoidRootPart
	end)
	rStorage.MiscEvents.RootPartAdded.Event:Wait()
       end
	for i, v in pairs(pService:GetPlayers()) do
		if v:WaitForChild("SettingsFolder").ShowParticles.Value then
rStorage.MiscEvents.AddParticle:FireClient(v,particlesMod[plr.SettingsFolder.Equipped.Value], RootPart)
		end
	end
end)

Edit: I honestly forgot about :FindFirstChild() when attempting to find something, idk how I missed that

if char.HumanoidRootPart == nil then
For what it’s worth, both you and OP have this in your code, and unless anything’s changed it will error if humanoid root part can’t be found. This sort of thing requires find first child

1 Like

Where is the local script located?

StarterPlayerScripts, so it would be in my player instance in PlayerScripts

I had the same kind of issue where my function would only work on spawn, try moving it to StarterCharacterScripts?

So I tried this out and added some print debugging and the output was interesting…

--Server code:
if c.HumanoidRootPart == nil then
	print("rootpart is nil")
	c.ChildAdded:Connect(function(child)
		if child.Name =="HumanoidRootPart" then
			rootpart=child;rStorage.MiscEvents.RootPartAdded:Fire()
		end
	end)
	rStorage.MiscEvents.RootPartAdded.Event:Wait()
else
	print('rootpart is not nil, setting to humanoidrootpart')
	rootpart=c.HumanoidRootPart
end
--Client code:
rStorage.MiscEvents.AddParticle.OnClientEvent:Connect(function(pInfoTable,rootpart)
	print('client function firing', tostring(rootpart))
--Output:
  09:45:01.584  rootpart is not nil, setting to humanoidrootpart  -  Server - MainServerScript:25
  09:45:01.622  client function firing nil  -  Client - ClientStarterPlayer:27

Do you think it could be an issue where the server is loading the character before the client?

I believe you need to call FindFirstChild first when attempting to find something that could potentially be equal to nil as @JarodOfOrbiter mentioned (Thanks btw), attempting to reference a child inside the Character without implementing any sanity checks would make the script “assume” that there’s a HumanoidRootPart already inside that Character

I did edit my script if you want to try that one

I still get the same output where the part is not nil on the server but is nil when entered into the remote event.

Maybe try printing rootpart on the server side? & another print in the ChildAdded Event

I changed my script a bit, here’s an interesting one:

--Server:
plr.CharacterAdded:Connect(function(c)
	for i, v in pairs(pService:GetPlayers()) do
		if v:WaitForChild'SettingsFolder'.ShowParticles.Value then
			rStorage.MiscEvents.AddParticle:FireClient(v, particlesMod[plr.SettingsFolder.Equipped.Value], plr)
			print("fired")
		end
	end
end)

--Client
rStorage.MiscEvents.AddParticle.OnClientEvent:Connect(function(pInfoTable,plr)
	local c = plr.Character or plr.CharacterAdded:Wait(); local rootpart = c.HumanoidRootPart or c:WaitForChild("HumanoidRootPart")
	print(tostring(rootpart))
print(tostring(particles.Parent),tostring(particles.Parent.Parent),tostring(particles.Parent.Parent.Parent))

When I first spawn in, it gives me particles and prints
HumanoidRootPart Meisrcool Workspace
Then when I respawn it prints
HumanoidRootPart Meisrcool nil
Any ideas which Meisrcool it’s referencing?

Not exactly sure, you may need to find where the particles variable exactly is from the client side