luaroblox

Getting error in attempt to index nil with a shop GUI


I have this shop GUI in Roblox that keeps giving me an error when I try and buy something. I have the link to the model so you can look at it and try and solve the problem. This is the code where things are going wrong.

local price = script.Parent.Parent.Price
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local tool = tools:FindFirstChild(script.Parent.Parent.ItemName.Value)
local player = script.Parent.Parent.Parent.Parent.Parent.Parent

script.Parent.MouseButton1Click:connect(function()
    if player.leaderstats:FindFirstChild("Coins").Value >= price.Value then -- Change "Money" to the name of your currency. Example: Cash
        player.leaderstats:FindFirstChild("Coins").Value = player.leaderstats:FindFirstChild("Coins").Value - price.Value
        local clone = tool:Clone()
        clone.Parent = player.Backpack
        -- Copy this code if you want the player to still have the tool even if he died
        local clone2 = tool:Clone()
        clone2.Parent = player.StarterGear
    end
end)

This is the error it gives me:

10:16:49.715 - Players.kaitheawsomecathoo1.PlayerGui.Shop.Frame.Info.Buy.Script:9: attempt to index nil with 'Clone'
10:16:49.715 - Stack Begin
10:16:49.715 - Script 'Players.kaitheawsomecathoo1.PlayerGui.Shop.Frame.Info.Buy.Script', Line 9
10:16:49.716 - Stack End

Here is the link to the model.


Solution

  • tool is nil.

    Find out why and only call tool:Clone() if tool is a valid value.