Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to i fix this Workspace.asdxc.Script:8: attempt to index a nil value?

Asked by 5 years ago
debounce = true

function onTouched(hit)
        local h = Instance.new("Hat")
        local p = game.Workspace.pet:Clone()
        h.Name = "GreenTopHat"
        p.Parent = h
        p.Position = hit.Parent:findFirstChild("Head").Position -- this is a nill??
        p.Name = "Handle" 
        p.Locked = true 
        h.Parent = hit.Parent
        h.AttachmentPos = Vector3.new(0,0,1)
    end
script.Parent.Touched:connect(onTouched)

0
Whatever is hitting the part doesn't have a head, you should do an if statement on the findfirstchild and then if that returns true then change the position Vulkarin 581 — 5y
0
You can't instance a hat... SBlankthorn 329 — 5y
0
You can, only the Hat class is deprecated, OP should be creating Accessory objects instead. User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

This is because Head wasn't found in hit.Parent. You should instead have an if statement that checks for it.

local debounce = true -- use local variables

local function onTouched(hit) -- here too
    if hit.Parent:FindFirstChild("Head") then -- :findFirstChild is deprecated, use :FindFirstChild
        local acc = Instance.new("Accessory") -- Hat is deprecated
        local p = game.Workspace.pet:Clone()
        p.Name = "GreenTopHat"
        p.Position = hit.Parent.Head.Position -- this only runs if head was found !!
        p.Name = "Handle" 
        p.Locked = true 
        p.Parent = hit.Parent

        h.AttachmentPos = Vector3.new(0,0,1)
        h.Parent = hit.Parent
    end
end
script.Parent.Touched:Connect(onTouched) -- :connect is deprecated, use:Connect


0
oui Vulkarin 581 — 5y
Ad

Answer this question