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

Whats wrong with this script?

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)

2 answers

✓ Accepted answer

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)


3 comments

0

Didn't work.

0

Oh I see, on line 13 change it to this: p.CFrame = CFrame.new(char:findFirstChild("Head").CFrame.p)

0

Still didn't work.

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