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 make it debounce correctly and make it spawn one time?

i couldnt do it

local part = game.Workspace.door
local part2 = game.Workspace.Part
local part3 = game.Workspace.Parte
local replica = game.Workspace.replic


part.touched:Connect(function(hit)
	if hit.parent:FindFirstChild("Humanoid") then
			wait(0.1)
			
			local imrandom = math.random(1,2)
		print(imrandom)
		

		
		if imrandom == 1 then
			

			local resetSeconds = 1 
			local isTouched = false

			if not isTouched then
				isTouched = true
			end

			wait(resetSeconds)
			isTouched = false
            			
				local door = Instance.new("Part", workspace)
				door.Transparency = replica.Transparency
				door.Material = replica.Material
			    door.Position = part.Position
			    door.Size = replica.Size
			
			else
				for i = 0,1, .01 do 
					wait()
					part.CFrame = part.CFrame:Lerp(part2.CFrame, i)
				
			end
			
			
			
		end
		
         
			
						
		
			
			
		
		
		
		end

	end)

help

1 comment

0

amazing whitespace

1 answer

✓ Accepted answer

Here's an example of how a debounce is implemented in a code. Remember that debounces are only there to prevent a code from running too many times.

##Fixed Code:

local part = game.Workspace.door
local part2 = game.Workspace.Part
local part3 = game.Workspace.Parte
local replica = game.Workspace.replic
local debounce = true

part.Touched:Connect(function(hit)
	if hit.parent:FindFirstChild("Humanoid") and debounce then
		
		debounce = false
		wait(1) -- change to the amount of seconds you want this to elapse
		local imrandom = math.random(1,2)
		print(imrandom)



		if imrandom == 1 then


			local resetSeconds = 1 
			local isTouched = false

			if not isTouched then
				isTouched = true
			end

			wait(resetSeconds)
			isTouched = false
			debounce = true
			local door = Instance.new("Part", workspace)
			door.Transparency = replica.Transparency
			door.Material = replica.Material
			door.Position = part.Position
			door.Size = replica.Size

		else
			for i = 0,1, .01 do 
				wait()
				part.CFrame = part.CFrame:Lerp(part2.CFrame, i)

			end

		end
	end

end)

##Recommendation(s):

  • Lua is case sensitive so :touched should be :Touched. :touched is not an event.

Sorry for not answering sooner, comment if you have more questions or replies!

Fixed minor errors!

1 comment

0

change the wait() to the time you want it to run.

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