roblox-studio

attempt to index nil with 'Value'


I am trying to make a tycoon in roblox but it said:

Workspace.Tycoons.Tycoon.MainItems.ConveyorBelt.CashPart.Script:5: attempt to index nil with 'Value'

This is my script:

local values = script.Parent.Parent.Parent.Parent.Values
script.Parent.Touched:Connect(function(hit)
    if hit.Name == "DropperPart" and hit:IsA("BasePart") then
        values.MoneyValue.Value += hit:FindFirstChild("CashValue").Value
        hit:Destroy()
    end
end)

and this is line 5: values.MoneyValue.Value += hit:FindFirstChild("CashValue").Value

i tried fixing it but i can't can someone help me


Solution

  • It’s saying it’s nil which means it probably doesn’t know the child exists, so you can’t access its value. Try this:

    local values = script.Parent.Parent.Parent.Parent.Values
    
    script.Parent.Touched:Connect(function(hit)
    local cashValue = hit:FindFirstChild("CashValue")
    
    if hit.Name == "DropperPart" and hit:IsA("BasePart") and cashValue then
        values.MoneyValue.Value += cashValue.Value
        hit:Destroy()
    end
    end)