Reputation: 1
Hello i am trying to get the position of the HumanoidRootPart but it says attempt to index nil with 'Character' also the FollowPlrbox has the players name.(This not a local script) Script:
script.Parent.MouseButton1Click:Connect(function()
local fwbox = script.Parent.Parent.FollowPlrbox.Text
local player = game.Workspace:FindFirstChild(fwbox)
local plrpart = player.Character:FindFirstChild("HumanoidRootPart")
local plrposx = plrpart.Position.X
print(plrposx)
end)
Error: attempt to index nil with 'Character'
Upvotes: 0
Views: 705
Reputation: 7188
Your error is telling you that player
doesn't exist. This could mean that whatever you have typed for the player's name is misspelled, incorrect capitalization, or there's simply no player with that name.
Anyways, you need to account for the fact that the player might not exist before trying to access properties on it. Also, since you are searching the Workspace, you are not looking for a Player, you are looking for that player's character model.
So to fix your issue, you simply need to add safety checks.
script.Parent.MouseButton1Click:Connect(function()
local fwbox = script.Parent.Parent.FollowPlrbox.Text
local character = game.Workspace:FindFirstChild(fwbox)
if character then
local rootPart = character:FindFirstChild("HumanoidRootPart")
if rootPart then
local rootPartPosX = rootPart.Position
print(rootPartPosX)
end
end
end)
The reason we add if character then
and if rootPart then
is because it's possible that the FindFirstChild()
function won't find anything. So we say, only do the rest of this code if character exists.
Upvotes: 1