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:
01 | game.Loaded:Wait() -- To prevent loading issues |
02 |
03 | local player = game.Players.LocalPlayer |
04 | local char = player.Character |
05 | local rayPart = game.ReplicatedStorage.RayPart:Clone() |
06 | rayPart.Parent = workspace |
07 |
08 | game.ReplicatedStorage.GiveAbilityToTransfurm.OnClientEvent:Connect( function () |
09 | spawn( function () |
10 | while wait() do |
11 | rayPart.CFrame = char.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new( 0 , 0 , 5 )) |
12 | end |
13 | end ) |
14 | end ) |
I am not very experienced with CFrame, so this could be a very simple fix.
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.
Instead of game.Loaded:Wait() you could use
1 | player.CharacterAdded:Wait() |