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

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

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)

3 comments

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

0

You can't instance a hat...

0

You can, only the Hat class is deprecated, OP should be creating Accessory objects instead.

1 answer

###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


1 comment

0

oui

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