Attempt to index nil with 'parent'

So i want this script to make it so when you equip a different axe it remvoes the other one and checks to see if the other one is in the backpack or the players hand and remove it and replace it with the other one that they chose, but i dont know what this error is saying?

local rp = game:GetService("ReplicatedStorage")
local axe = rp.Axes.Diamondaxe.Axe
local plr = game:GetService("Players")
local player = plr.LocalPlayer

local function checktool()
	for _, child in pairs(player.Character:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy() 
			local newaxe = axe:Clone()
			newaxe.Parent = player.Backpack
		end
	end
	return nil
end

local function checktool2()
	for _, child in pairs(player.Backpack:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy() 
			local newaxe = axe:Clone()
			newaxe.Parent = player.Backpack
		end
	end
	return nil
end

script.Parent.MouseButton1Click:Connect(function()
	checktool()
	checktool2()
end)

Try printing the variable axe. Just need to make sure that isn’t nil.

it is not nill, I have no idea what is going on

You are returning nil at the end of each function.

There is no need since you are not trying to make use of the returned value.

Example:

local ReturnedValue = checktoo() print(ReturnedValue)

This line of code is expecting a return value.

checktoo()

This line of code is not expecting a return value.

what would the return values be then?

I know I’m not @mc7oof, but you can try this. Instead of return nil, use return newaxe instead, and now check if it prints.

You have return nil at the end of each function.

It doesn’t look like you need a return value. So, just delete return nil at the end of the two functions.

I already dealted them but it still says attempt to index nil with parent

Screen capture the error so we know what line it is on.

Then screen capture the code block with the matching line.

18:26:43.635 Players.TheReal_MrMonkey.Backpack.Axe.Handle.Script:11: attempt to index nil with ‘Parent’ - Studio
18:26:43.635 Stack Begin - Studio
18:26:43.635 Script ‘Players.TheReal_MrMonkey.Backpack.Axe.Handle.Script’, Line 11 -

	local newaxe = axe:Clone()

That means the item it is trying to clone does not exist.

So look at the line that defines the item:

local axe = rp.Axes.Diamondaxe.Axe

This item is not being found.

1 Like

Going off of the logical explanation based on the error here, the variable newaxe is nil. Lua is complaining that you are trying to index the nil value inside your newaxe variable with .Parent.

how would i define the item then?

You probably just need to add a WaitForChild.

local axe = rp:WaitForChild("Axes"):WaitForChild("Diamondaxe"):WaitForChild("Axe")

It still is saying index nil with parent

Is the script you are showing in this post:

Backpack.Axe.Handle.Script?

no it is not, ill send the srcipt that is having the problems

local rp = game:GetService("ReplicatedStorage")
local axe = rp:WaitForChild("Axes"):WaitForChild("Diamondaxe"):WaitForChild("Axe")
local plr = game:GetService("Players")
local player = plr.LocalPlayer

local function checktool()
	for _, child in pairs(player.Character:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy() 
			print(axe)
			local newaxe = axe:Clone()
			newaxe.Parent = player.Backpack
		end
	end
end

local function checktool2()
	for _, child in pairs(player.Backpack:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy() 
			local newaxe = axe:Clone()
			newaxe.Parent = player.Backpack
		end
	end
end

script.Parent.MouseButton1Click:Connect(function()
	checktool()
	checktool2()
end)

Right, the script that is inside the handle is the one that has an error on line 11.

local handle = script.Parent
local Players = game:GetService("Players")
local goi = Players.LocalPlayer.PlayerGui.Gui
local Tween = game:GetService("TweenService")
local Replicatedstorage = game:GetService("ReplicatedStorage")
local Woodadd = Replicatedstorage.Wood
local woodPosition = UDim2.new(0.253, 0,-0.028, 0)
local tweenInfo = TweenInfo.new(2)
local leaderstats = Players.LocalPlayer:WaitForChild("leaderstats")
local wood = leaderstats.Wood
local damage = script.Parent.Parent.damage
local canSwing = true
db = true
dbbb = true

for i, v in pairs(handle.Parent:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
		v.Touched:Connect(function(hit)
			if hit.Parent.Name == "Tree"and db == true  and not canSwing and hit.Parent.Canbeattacked.Value == false then
				local amount

				if goi:FindFirstChildOfClass("IntValue") then -- since you parent your int value to the goi thing I checked that

				else
					amount = Instance.new("IntValue")
					amount.Parent = goi
				end

				amount = goi:FindFirstChildOfClass("IntValue")

				local tree = hit.Parent
				local modelCFrame = tree:GetPivot()
				local health = tree.Health
				local DamageDelt = tree.DamageDelt
				db = false

				local oldpos = tree.Tree.CFrame.Position

				local goal = {CFrame = script.Parent.CFrame * (CFrame.new(1,1,1) * CFrame.Angles(math.rad(75),0,0))}
				local tweenlog = Tween:Create(tree.Tree,tweenInfo,goal)

				goi.CanvasGroup.Visible = true
				health.Value -= damage.Value
				DamageDelt.Value += damage.Value
				amount.Value += 1

				local function wodas()
					goi.CanvasGroup.Amount.Size = UDim2.new(DamageDelt.Value/100,0,0,75)
					local random = math.random(1,90)
					local xnew = random/100
					local wood2 = game.ReplicatedStorage.Wood:Clone()
					wood2.Parent = Players.LocalPlayer.PlayerGui.Items
					wood2.Position = UDim2.new(xnew,0,0.76,0)
					wood2.TextLabel.Text = "+"..damage.Value
					local tween = Tween:Create(wood2, tweenInfo, {Position = woodPosition})
					tween:Play()
					wait(1.3)
					wood2:Destroy()
				end

				if health.Value == 0 or health.Value < 0 then
					print(health.Value)
					wodas()
					tweenlog:Play()
					goi.CanvasGroup.Visible = false
					goi.CanvasGroup.Amount.Size = UDim2.new(0.2,0,0,75)
					amount:Destroy()
					game.ReplicatedStorage.AddWood:FireServer()
					hit.Parent.Canbeattacked.Value = false
					dbbb = false
					task.wait(20)
					tree.Tree.CFrame = CFrame.new(oldpos)
					dbbb = true
					hit.Parent.Canbeattacked.Value = true
					return
				end

				if health.Value <= 100 and dbbb == true then
					goi.CanvasGroup.Amount.Size = UDim2.new(DamageDelt.Value/100,0,0,75)
					wodas()
					game.ReplicatedStorage.AddWood:FireServer()
				end


				task.wait(1)
				db = true

			else
				print(hit.Parent.Name)
			end	
		end)
	end
end


handle.Parent.Activated:Connect(function()
	if canSwing then
		canSwing = false

		task.wait(1)

		canSwing = true
	end
end)

Is this line of code on line 11:

local damage = script.Parent.Parent.damage