What is wrong with this script? I'm trying to make it so when you click the hat it gives you it on your head, I've been trying for hours, whats wrong?
function onClicked() --code if onClicked(PlayerWhoClicked) then h = Instance.new("Hat") p = Instance.new("Part") h.Name = "Headphones" p.Parent = h p.Position = hit.Parent:findFirstChild("Head").Position p.Name = "Handle" p.formFactor = 0 p.Size = Vector3.new(2, 1, 1) p.BottomSurface = 0 p.TopSurface = 0 p.Locked = true script.Parent.Mesh:clone().Parent = p h.Parent = hit.Parent h.AttachmentPos = Vector3.new(0, 0.3, 0) wait(5) debounce = true end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
This code would crash the first time the onClicked
function ran because it is calling itself recursively forever. PlayerWhoClicked is also undefined.
Your code is kind of all messed up. Anyway, I fixed it now so here you go:
local debounce = true function onClicked(player) if debounce then debounce = false local char = player.Character h = Instance.new("Hat") p = Instance.new("Part") h.Name = "Headphones" p.Parent = h p.Size = Vector3.new(2, 1, 1) p.Position = CFrame.new(char:findFirstChild("Head").CFrame.p) p.Name = "Handle" p.FormFactor = "Smooth" p.BottomSurface = "Smooth" p.TopSurface = "Smooth" p.Locked = true script.Parent.Mesh:clone().Parent = p h.Parent = char h.AttachmentPos = Vector3.new(0, 0.3, 0) wait(5) debounce = true end end script.Parent.ClickDetector.MouseClick:connect(onClicked)