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

How do I change this script from an onTouched function to an onClicked function?

Hello!

For context:

Previously, players in my game had to approach the 'giver part' to give their player an "accessory" (part with a mesh inserted into the player's character, i.e "Top Hat"). This worked fine until recently, when it was discovered that it was easier to have the 'giving part' clickable. This is where I'm having trouble, since I don't know how to correctly modify the script so that I can update the function...

Here's the script:


local debounce = true

function onTouched(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
	if (hit.Parent:findFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
local	h = Instance.new("Hat")
local	p = Instance.new("Part")
		h.Name = "Top Hat"
		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, 0)
		wait(5)
		debounce = true
	else
		end
	end
end
script.Parent.Touched:connect(onTouched)

Any help is appreciated, thank you. :)

2 answers

✓ Accepted answer

You can use a ClickDetector in this case.

To me it looks like you are adding a hat to a player so I just used Humanoid:AddAccessory. However, if that is not the case you can just erase it and add your own code.

local clickDetector = script.Parent:FindFirstChild("ClickDetector") -- click detector
local hat = game.Workspace:FindFirstChild("Top Hat") -- hat


local function clicked(player)
	local playerinworkspace = game.Workspace:FindFirstChild(player.Name) -- find player in workspace
	local playerhumanoid = playerinworkspace:FindFirstChild("Humanoid") -- find player humanoid
	
	local newhat = hat:Clone() -- clone hat
	newhat.Handle.Anchored = false -- unachor hat
	
	playerhumanoid:AddAccessory(newhat) -- add accessory
end

clickDetector.MouseClick:Connect(clicked)

You can also add a if statement to check if the player already has a hat so that they won't have a duplicate.

local checkhat = playerinworkspace:FindFirstChild("Top Hat") -- the name of our hat
if checkhat then return end

1 comment

2

NOO YOU ANSWERED WHILE IM TYPING MY ANSWER :'(

First, create a ClickDetector inside the Part (if you already did you can proceed to next step).

Second, use the ClickDetector.MouseClick which fires when a Player clicks on the Part.

Third, modify the Script.

local Players = game:GetService("Players")
local part = script.Parent
local clickDetector = part.ClickDetector
local debounce = true

local function onClicked(player)
	if debounce == true then
		debounce = false
		
		local character = player.Character
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		local head = character.Head
		
		local mesh = part.Mesh
		local meshClone = mesh:Clone()
		
		local hat = Instance.new("Accessory")
		hat.Name = "Top Hat"
		hat.AttachmentPos = Vector3.new(0, 0, 0)
		
		local handle = Instance.new("Part", hat)
		handle.Name = "Handle"
		handle.Size = Vector3.new(2, 1, 1)
		handle.Material = Enum.Material.SmoothPlastic
		handle.Locked = true 
        	meshClone.Parent = handle
		handle.Position = head.Position
		
		humanoid:AddAccessory(hat) -- makes the player wear the hat
		task.wait(5)
		debounce = true
	end
end

part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
	if player ~= nil then -- if player exists (not nil/nonexistent)
		onClicked(player)
	end
end)

clickDetector.MouseClick:Connect(onClicked)

And that's it! It should work now! :D

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