Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Line 10 reads "attempt to index nil with 'Clone' why is this?

local LevelReq = script.Parent:WaitForChild("LevelReq").Value
local Price = script.Parent:WaitForChild("Price").Value

script.Parent.MouseButton1Click:Connect(function()
	local Player = game.Players.LocalPlayer
	local Stats = Player:WaitForChild("leaderstats")
	local Level = Stats:WaitForChild("Level")
	local Gold = Stats:WaitForChild("Gold")
	local Tool = script.Parent.Item.Value
	local CTool = game.ReplicatedStorage.ToolLibrary:FindFirstChild(Tool):Clone()
	if Player.Backpack:FindFirstChild(Tool) or Player.Character:FindFirstChild(Tool) then return end
	if Level.Value >= LevelReq then
		if Gold.Value >= Price then
			Gold.Value = Gold.Value - Price
			CTool.Parent = Player.Backpack
		end
	end
end)

script.Parent.MouseEnter:Connect(function()
	local Cost = script.Parent.Parent.Parent:WaitForChild("Cost")
	Cost.Text = "Cost: 3"..Price
end)

script.Parent.MouseLeave:Connect(function()
	local Cost = script.Parent.Parent.Parent:WaitForChild("Cost")
	Cost.Text = "Cost:"
end)

I've been pulling my hair out trying to fix the error on Line 10, the shop I've made opens and closes, just buying a tool is the part that doesn't work.

2 answers

✓ Accepted answer

Hello, DynamicPlanet!

You should use WaitForChilld() to force your script to wait until the child is created (or until your script is killed by Roblox, if the child don't get created in a long time)

local LevelReq = script.Parent:WaitForChild("LevelReq").Value
local Price = script.Parent:WaitForChild("Price").Value

script.Parent.MouseButton1Click:Connect(function()
    local Player = game.Players.LocalPlayer
    local Stats = Player:WaitForChild("leaderstats")
    local Level = Stats:WaitForChild("Level")
    local Gold = Stats:WaitForChild("Gold")
    local Tool = script.Parent.Item.Value
    local CTool = game.ReplicatedStorage.ToolLibrary:WaitForChild(Tool):Clone() --Change here
    if Player.Backpack:FindFirstChild(Tool) or Player.Character:FindFirstChild(Tool) then return end
    if Level.Value >= LevelReq then
        if Gold.Value >= Price then
            Gold.Value = Gold.Value - Price
            CTool.Parent = Player.Backpack
        end
    end
end)

script.Parent.MouseEnter:Connect(function()
    local Cost = script.Parent.Parent.Parent:WaitForChild("Cost")
    Cost.Text = "Cost: 3"..Price
end)

script.Parent.MouseLeave:Connect(function()
    local Cost = script.Parent.Parent.Parent:WaitForChild("Cost")
    Cost.Text = "Cost:"
end)

2 comments

1

Thank you very much! Gosh, I seem really stupid for not realizing that error there.

1

Don't worry, i'm here to help

Try doing


local CTool = game.ReplicatedStorage.ToolLibrary:FindFirstChild(Tool)

Then add a line after this and put

local Cloned = CTool:Clone()

8 comments

0

The local CTool = game.ReplicatedStorage.ToolLibrary:FindFirstChild(Tool) ends up having a red line under (Tool). It also ended up outputting nothing in output.

0

Oh I know, you have ‘tool’ set as a Value that’s why.

0

local Tool = script.Parent.Item.Value the tool shouldn’t be a value.

0

Hmm.. I'm still stumped on what to do. Changing the value around doesn't seem to fix the problem and just causes more errors in my script.

0

Change Tool to local Tool = script.Parent.Item

0

Or wherever your tool is

0

Changed the tool to the path, yet the (Tool) is still showing up as missing or nil.

0

Nevermind, moved my local Tool path and it fixed it, yet the (Clone) in my script is showing as index nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.