Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

attempt to index nil with 'HumanoidRootPart'?

Asked by 4 years ago

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.

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. JesseSong 3916 — 4y
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. JesseSong 3916 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your 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.

0
jerry is right Windsonnes 75 — 4y
0
Downvoted because of lack of explanation! JesseSong 3916 — 4y
0
Jesse, chill I understannd why this is the solution. deeskaalstickman649 475 — 4y
0
Thanks! I just misunderstood the error. deeskaalstickman649 475 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

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

player.CharacterAdded:Wait()
0
Downvoted because of lack of explanation! JesseSong 3916 — 4y

Answer this question