Touched function broke after running perfectly the first time

  1. What do you want to achieve? Keep it simple and clear!
    Simply put, tried to make a script that when a part (lets call it part A) is touched, it will run a countdown for loop. When the countdown ends, part B’s properties will change. When you touch part A again after part B’s property change already ran, part B’s properties will change again.

Code i’m having issues with:

local startLine = script.Parent
local startPosition = game.Workspace.StartEr
local countDown = game.Workspace.CountDown
local countText = countDown.SurfaceGui.TextLabel
local winnerName = game.Workspace.WinnerName
local winnerText = winnerName.SurfaceGui.TextLabel
countText.Text = ("Countdown hasnt started")

local countdownDuration = 5
local startOn = false
local countdownOn = false
local startTouch = false

local function startCount()
	
	for counting = countdownDuration, 0, -1 do
		countdownOn = true
	    countText.Text  = counting
		wait(1)
	end	
	
	startOn = true
	   startLine.Transparency = 0.6
	   startLine.CanCollide = false
	countText.Text = ("GAME START")
	winnerText.Text = ("Who will become the champion?")
	print("OPEN THE GAME!")
	
	wait(4)
	
	countText.Text = ("WE ARE HYPE OUT HERE TODAY!")
	
	wait(4)
	
	if startOn == true then
		startPosition.Touched:Connect(function(touchoff)

			local humanoid = touchoff.Parent
			local realHuman = humanoid:FindFirstChildWhichIsA("Humanoid")

			if humanoid and startOn == true then
				startOn = false
				print("hoh")
				startLine.Transparency = 0
				startLine.CanCollide = true
				countText.Text = ("Game End YARR YARR")
				startLine.Transparency = 0
				winnerText.Text = (realHuman.Parent.Name.." has won")
			end
			
			wait(3)
			countText.Text = ("Countdown hasnt started")
			countdownOn = false
			startTouch = false
			end)
	end
end

startPosition.Touched:Connect(function(touchings)

	local human = touchings.Parent
	local fullHuman = human:FindFirstChildWhichIsA("Humanoid")

  if fullHuman and startTouch == false then
		startTouch = true
		print("accurate condition")
		startCount()

	end

end)
  1. What is the issue? Include screenshots / videos if possible!
    The issue appears only after i run the function for the first time, and if i try to make it run again by touching the startPosition part, it will run the script with the “acccurate condition” print multiple times and ruin the script. So i want to make it actually run without any problems the second time i touch it.
    Video : robloxapp-20210511-1621301.wmv (6.9 MB)
    (mind the bad quality)
    Printing to prove that it happened multiple times : 2021-05-11 (2)
    (issues happened after second accurate condition print)

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried playing with the bool values, because that’s the only thing thats keeping the scripts from running multiple times.

This code being inside a function that gets called multiple times is dangerous - each time the function is called, it will create a new connection, which is bad for performance and also for other obvious reasons like the code inside it running more than once. This should be outside of the function, and the if statement regarding the startOn variable should be inside the touched event. The touched event will fire multiple times always, it’s up to you to add a debounce or to use if statements to determine under what circumstances the code should be ran.

You forgot to set startTouch to false after startCount()

this right here is the issue, If you set it as a variable then disconnect it after it happened

if startOn == true then
		local Event
		Event = startPosition.Touched:Connect(function(touchoff)
			Event:Disconnect()
			local humanoid = touchoff.Parent
			local realHuman = humanoid:FindFirstChildWhichIsA("Humanoid")

			if humanoid and startOn == true then
				startOn = false
				print("hoh")
				startLine.Transparency = 0
				startLine.CanCollide = true
				countText.Text = ("Game End YARR YARR")
				startLine.Transparency = 0
				winnerText.Text = (realHuman.Parent.Name.." has won")
			end
			
			wait(3)
			countText.Text = ("Countdown hasnt started")
			countdownOn = false
			startTouch = false
			end)
	end

that MIGHT work but is just an example.

1 Like