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

Multiple clones on 1 touch???

This is what's happening https://gyazo.com/c87afac7a2c901e4acfaea31d6f60a5a

this is my script

p = script.Parent
function off (hit)
	local e = hit.Parent
	local humanoid = e:FindFirstChildWhichIsA("Humanoid")
	if ( humanoid ) then
	b = game.ServerStorage.a:Clone()
	b.Parent = game.Workspace
	end

end

p.Touched:Connect(off)

I only want it to clone once when I'm touching the the scripts parent and only to clone another if I stopped touching the part and retouched it.

What am I doing wrong?

4 comments

0

use debounce..

0

Could you give me an example?

0

sure..! local a = true if a == true then a = false else a = true

0

also, that might be because multiple parts of a player are touching the part

1 answer

You can debounce. A quick example of debouncing with the Part.Touched event:

local debounce = false
local somePart = workspace.Part

somePart.Touched:Connect(function(hit)
	if not debounce then

		-- now we just set debounce to true
		-- this prevents the code below from being executed too quickly
		debounce = true
		
		-- put your code after that
		print(hit.Name .. ' touched a part!')

		-- then we wait (in this example, one second)
		-- and we set debounce back to false
		wait(1)
		debounce = false
	end
end)

What does this do? Basically, by setting debounce to true, we are preventing the code from being executed too fast. The wait at the end is the delay until the code within the if block can be executed again.

4 comments

0

a more accurate way: if debounce == true then return end

0

"if not debounce then" is not guaranteed to work, trust me

0

MCAndRobloxUnited thanks for telling me, but can you elaborate?

0

ninja'd

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