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

Touched Script Ingnores Debounce. Can someone help me out please?

In my Roblox game, I'm trying to make a minigame. If a certain amount of "Microchips" are collected (for testing purposes I've set it to 1), the player gets a 20 Coin bonus. The problem is that the script starts multiple times leading to an exaggerated bonus. I've even tried switching the Touched event to TouchEnded. The script fires when the player touches the "GUI" part, the problem is that the Touched event fires multiple times leading to multiple scripts firing. I've implemented a debounce (guiPressed) however this had no effect on the script. I don't know what else I can do to solve this issue. This is probably a simple fix but I am fairly new to LUA programming. In my Local Script I implemented print() functions to see what is happening and as expected they were printed multiple times.

Local Script Code:

workspace.TimeTrials.GUI.Touched:Connect(function(hit)
	
	local guiPressed = false

	if guiPressed == false then
		
		guiPressed = true
		
		local plrChar = hit.Parent
		local plr = game.Players.LocalPlayer
		local frame = plr.PlayerGui.TimeTrials:FindFirstChild("Frame")
		local mainText = frame.Bottom.Main
			
		local yes1 = frame.TopButtons.Yes1
		local no1 = frame.TopButtons.No1
		
		local playing = script.Parent.Playing
			
		if frame.Position.Y.Scale >= 1 and playing.Value == false then
			
			frame.Top.Visible = false
			frame.TopButtons.Visible = true
			
			mainText.Text = "Hey! Would you like to compete in my time trial? If you collect enough microchips, I'll give you a little gift!"
		
			frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0)) 
		
			yes1.Active = true
			no1.Active = true
		
			yes1.MouseButton1Click:Connect(function()
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				plrChar.HumanoidRootPart.Anchored = true
				plrChar.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(-15.3, 20, 107.1))
				plrChar.HumanoidRootPart.Orientation = Vector3.new(0, 0, 0)
				
				mainText.Text = "Ok! Starting in 3"
				wait(1)
				mainText.Text = "Ok! Starting in 2"
				wait(1)
				mainText.Text = "Ok! Starting in 1"
				wait(1)
				
				local startMicrochips = plr.leaderstats.Microchips.Value
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				plrChar.HumanoidRootPart.Anchored = false
				
				wait(5)
					
				local finalMicrochips = plr.leaderstats.Microchips.Value
				local collectedMicrochips = finalMicrochips - startMicrochips
			
				if collectedMicrochips == 1 then
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchip!"
					
				else
					
					mainText.Text = "Time up! You collected " .. tostring(collectedMicrochips) .. " Microchips!"
					
				end
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 0.79, 0))
				
				if collectedMicrochips > 1 then
					
					wait(2.5)
					
					mainText.Text = "Wow! You collected a lot of Microchips! Here are 20 Coins as a gift from me!"
					plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 20
					
				end
				
				wait(2.5)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				playing.Value = false
				guiPressed = false
				
			end)
	
			no1.MouseButton1Click:Connect(function()
				
				frame.TopButtons.Visible = false
				frame.Top.Visible = true
				
				mainText.Text = "Ok, see you some other time!"
				
				wait(2)
				
				frame:TweenPosition(UDim2.new(frame.Position.X.Scale, 0, 1.1, 0))
				
				wait(1)
				
				guiPressed = false
				
			end)
	
		end
				
	end
	
end)

wait(2.5)

Thanks in advance.

2 answers

✓ Accepted answer

You need to move line 3 to be outside of the touched function, like this:

local guiPressed = false

workspace.TimeTrials.GUI.Touched:Connect(function(hit)
	if guiPressed == false then
		guiPressed = true
		-- etc, etc
	end
end)

The way you had it, the guiPressed variable was getting set to false every time the touched function ran, making the debounce not work.

1 comment

0

Oh! Thanks so much! I can't believe I didn't realize that sooner!

Put local guiPressed = false at the top of the script.

1 comment

0

Thanks for your help! :)

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