Attempt To Index nil with position error on BATIM axe

Im doing a kind of BATIM axe. which breaks planks. and the part of the script where it checks if you are in range gives me a Attempt To Index nil with position error. how do i fix this?

if target.Parent.Name == “BreakablePlank” and (Character:FindFirstChild(“HumanoidRootPart”).Position - target.Parent:FindFirstChild(“PositionPart”).Position).magnitude <= 10 then

[the target part is a variable which is Mouse.Target]

This error means that either Character:FindFirstChild(“HumanoidRootPart”) or target.Parent:FindFirstChild(“PositionPart”) doesn’t exist and is nil (likely the second one). Because of that, nil doesn’t have a property called Position (it doesn’t have any properties), and that error is raised. To prevent this error, you should make sure both of these parts exist, and use WaitForChild instead of FindFirstChild so the script can wait a little bit just in case that part appears in future.

i get an infinite yield error.

That means what you’re trying to retrieve doesn’t exist (you can tell what by the error). You should, as I said, double check if that instance exists, and if any script creating it works.

but it does exist
and im typing it right

Send me the infinite yield error. In addition, the object you’re trying to retrieve could be there when editing, but it could be gone on runtime.

my screenshot button doesnt work so ill just send script and error:

error:

Infinite yield possible on ‘Workspace.Planks.StrongPlanks.BreakablePlank:WaitForChild(“PositionPart”)’ - Studio
09:25:05.001 Stack Begin - Studio
09:25:05.001 Script ‘Players.CaptainTeemukas.Backpack.Axe.UseAxe’, Line 59 - Studio - UseAxe:59
09:25:05.001 Stack End

script :

–// Variables //–

– Other
local Tool = script.Parent
local Animation = Tool.Swing
local Cooldown = 0.8
local debounce = false

local AxeSwing = workspace.Sounds.Axe.Swing:GetChildren()
local randomSoundSwing = AxeSwing[math.random(1, #AxeSwing)]

–Damage
local Handle = script.Parent.Handle
local PlayersHit = {}

–// Script //–

Tool.Activated:Connect(function(Mouse)
if debounce then return end
debounce = true

– // Play Animation // –

local Character = Tool.Parent
local hrp = Character.Humanoid
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

local MaxDistance = 6
local origin = Character.HumanoidRootPart.Position
local direction = (Mouse.Hit.Position-origin).Unit
local ray = Ray.new(origin, direction*MaxDistance)

local AnimationTrack = hrp:LoadAnimation(Animation)

AnimationTrack:Play()
randomSoundSwing:Play()

– // Do Damage // –

Handle.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild(“Humanoid”) and Hit.Parent ~= Tool.Parent then

  	if debounce == true and PlayersHit[Hit.Parent] == nil then
  		Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(50)
  		PlayersHit[Hit.Parent] = true
  		wait(Cooldown)
  		PlayersHit[Hit.Parent] = nil
  	end
  end

end)

– // Cut planks // –

Mouse.Button1Down:Connect(function()
local target = Mouse.Target
if not Mouse.Target then return end

  if target.Parent.Name == "BreakablePlank" and (Character:FindFirstChild("HumanoidRootPart").Position - target.Parent:WaitForChild("PositionPart").Position).Magnitude <= 4.5 then

  	local planksounds = workspace.Sounds.Breakable.Planks:GetChildren()
  	local randomSound = planksounds[math.random(1, #planksounds)]

  	local LeftPlank = target.Parent:FindFirstChild("LeftPlank")
  	local RightPlank = target.Parent:FindFirstChild("RightPlank")

  	randomSound.Volume = 0.5
  	randomSound:Play()
  	LeftPlank.Anchored = false
  	RightPlank.Anchored = false
  	wait(0.5)
  	randomSound.Volume = 0
  end

end)

wait(Cooldown)
debounce = false
end)

Judging by the error, it means that there is no such part called “PositionPart” in BreakablePlank, when checking if the player is close enough. Did you mean to use .Position, or did you parent it to the wrong Instance?

there is a part called positionpart inside the breakable plank model
idk whats going on

also everything is named correctly

Can you send the explorer then?

i would but my screenshot is broken

Could you take a screenshot of the explorer hierarchy that shows us that PositionPart is, in fact, inside the BreakablePlank? You can press Print Screen or use the snipping tool if your screenshot button doesn’t work.

ok here is the explorer

Use this then:

for i, v in ipairs(target.Parent:GetDescendants()) do
 print(v.Name, v.Parent)
end

Send us output after you executed it.

nevermind.

Well are you sure that both BreakablePlanks objects have the PositionPart? Perhaps the game could be trying to find it in the second one, not first.

yeah both of them do. the second one is a copy of the first one

Can you print target.Name? Perhaps the problem is that it checks the wrong part.

In the if clause there’s a check if target.Parent is called BreakablePlank. That check returns true I assume.

ohhhhhh it prints left plank / right plank.