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

attempt to index nil with 'HumanoidRootPart'?

My goal here is to put the RayPart 5 units in front of the local characters HumanoidRootPart, relative to their orientation using CFrame:ToWorldSpace function, however, I get the error "attempt to index nil with 'HumanoidRootPart'" and I'm not really sure what it means. I think it means that nil is trying to read HumanoidRootPart, but that can't be, as the part does exist. The error is on line 11. LocalScript:

game.Loaded:Wait() -- To prevent loading issues

local player = game.Players.LocalPlayer
local char = player.Character
local rayPart = game.ReplicatedStorage.RayPart:Clone()
rayPart.Parent = workspace

game.ReplicatedStorage.GiveAbilityToTransfurm.OnClientEvent:Connect(function()
	spawn(function()
		while wait() do
			rayPart.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0, 0, 5))
		end
	end)
end)

I am not very experienced with CFrame, so this could be a very simple fix.

2 comments

0

All you have to do is define the character. The error was basically saying you're attempting to reference something that doesn't exist with a instance.

0

All you have to do is define the character. THE error was basically saying you're attempting to reference something that doesn't exist with a instance.

2 answers

✓ Accepted answer

local char = player.Character or player.CharacterAdded:Wait()

try this instead of : local char = player.Character

player.CharacterAdded:Wait() is waiting until is character loaded, "Attempt to index with HumanoidRootPart" means, the character doesnt had time to load and script doesnt knew what HumanoidRootPart is.

4 comments

0

jerry is right

0

Downvoted because of lack of explanation!

0

Jesse, chill I understannd why this is the solution.

0

Thanks! I just misunderstood the error.

Instead of game.Loaded:Wait() you could use

player.CharacterAdded:Wait()

1 comment

0

Downvoted because of lack of explanation!

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