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

Is there an error in this script?

Hello, I have recently had help to extend upon this script, yet it still does not function correctly. By clicking the player should receives a hat from the script, yet the player receives nothing. What may be a possible factor for this reason?


debounce = true

if not script.Parent:findFirstChild("ClickDetector") then
  local cd = Instance.new("ClickDetector")
  cd.Parent = script.Parent
end
	 
	script.Parent.MouseButton1Down:connect(function()
	local character = player.Character
	    if (character:findFirstChild("Humanoid") ~= nil and debounce == true) then
	        debounce = false
	        h = Instance.new("Hat")
	        p = Instance.new("Part")
	        h.Name = "Hat"  
	        p.Parent = h
	        p.Position = character:findFirstChild("Head").Position
	        p.Name = "Handle"
	        p.formFactor = 0
	        p.Size = Vector3.new(-0,-0,-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.4,-0.025)
	        wait(5)     debounce = true

	    end

	end)

2 answers

Since you're using a ClickDetector object, use the .MouseClick event instead.

debounce = true

local ClickDetector = script.Parent:FindFirstChild("ClickDetector")

if not ClickDetector then
	ClickDetector = Instance.new("ClickDetector", script.Parent)
-- I polished the script a little bit. The second argument is where you want to parent the new instance
end

ClickDetector.MouseClick:connect(function(Player)
-- "MouseClick" is a member of ClickDetector objects
-- The player instance is returned when the ClickDetector detects a click from the player
	local Character = Player.Character
	if Character and Character:FindFirstChild("Humanoid") and debounce and Character:FindFirstChild("Head") then
		debounce = false
		h = Instance.new("Hat", Character)
		p = Instance.new("Part", h)
		h.Name = "Hat"
		p.Position = Character.Head.Position
		p.Name = "Handle"
		p.FormFactor = 0
-- This property is case sensitive!
		p.Size = Vector3.new(0, 0, -1)
		p.BottomSurface = 0
		p.TopSurface = 0
		p.Locked = true
		script.Parent.Mesh:Clone().Parent = p
		-- h.Parent = hit.Parent
-- What is "hit?" I decided to parent "h" to the Character in line 16
		h.AttachmentPos = Vector3.new(0,0.4,-0.025)
		wait(5)
		debounce = true
	end
end)

Thank you for your help, but there is just one problem after that. That part is cloned on your head, could there be a way to wear it? As a normal hat, because it clones on top of your head then falls as you move.

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