Function triggering multiple times

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    function triggering once per body part touched
  2. What is the issue? Include screenshots / videos if possible!
    it triggers multiple times
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried debounce it didnt work
    code:
	for _, part in game.Workspace:GetChildren() do
		if part.Name == "beam" then
			print(beamnum)
			beamnum = round(beamnum + 1)
			beam = "beam"..beamnum
			part.Name = beam
			part:WaitForChild("hi").Touched:Connect(function(hit)
				task.wait()
				dskl = dskl + 1
				print(dskl)
				hinum = part.Name
				kamehameha(hit, bonniecheck, freddycheck)
				gold = round(gold + scott)
			end)
			part:WaitForChild("hi").TouchEnded:Connect(function(hit)
				task.wait()
				dskl = dskl + 1
				hinum = part.Name
				kamehameha(hit, freddycheck, bonniecheck)
				gold = round(gold - scott)
			end)
		end
	end

dski prints hundereds of times. the code used to work fine but now it doesn’t work for some reason.

this is the problem because dskl is printing a lot. I need the function to run everytime the player touches it because the kamehameha function adds 1 if a player part is touched.

3 Likes

Try this:

local debounce = false
part:WaitForChild("hi").Touched:Connect(function(hit)
	if debounce then return end
	debounce = true
	task.wait()
	dskl = dskl + 1
	print(dskl)
	hinum = part.Name
	kamehameha(hit, bonniecheck, freddycheck)
	gold = round(gold + scott)

	debounce = false
end)
part:WaitForChild("hi").TouchEnded:Connect(function(hit)
	if debounce then return end

	task.wait()
	dskl = dskl + 1
	hinum = part.Name
	kamehameha(hit, freddycheck, bonniecheck)
	gold = round(gold - scott)
end)
2 Likes

I did it and it didnt work.
I dont think debounce gonna solve it because the code is instant and meant to be instant

for _, part in game.Workspace:GetChildren() do
		if part.Name == "beam" then
            local da = true
			print(beamnum)
			beamnum = round(beamnum + 1)
			beam = "beam"..beamnum
			part.Name = beam
			part:WaitForChild("hi").Touched:Connect(function(hit)
           	 if da == true then
	            da = false
				task.wait()
				dskl = dskl + 1
				print(dskl)
				hinum = part.Name
				kamehameha(hit, bonniecheck, freddycheck)
				gold = round(gold + scott)
	            end
			end)
			part:WaitForChild("hi").TouchEnded:Connect(function(hit)
            if da == false then
                da == true
				task.wait()
				dskl = dskl + 1
				hinum = part.Name
				kamehameha(hit, freddycheck, bonniecheck)
				gold = round(gold - scott)
                end
			end)
		end
	end

but still it will fire multiple times because anything that touch it will fire it
So you will need somthing to detect if player touched it

I didnt try this one.
but the part.touched thing is meant to run multiple times but for some reason its going crazy this time

Like the first poster mentioned, you need to add a debounce to prevent the code from running multiple times upon part touch. You can then add a task.wait(1) for the code to wait 1 second before setting debounce back to false to prevent the code firing multiple times when being touched.

is it the only way? my code wont even function if I do that

can you show a example of your current code?
It should be working fine if you are setting the debounce back after the rest of your code runs.

Also try not to repeat code if you have the chance, it can cause unintended problems.

local debounce = true
for _, part in game.Workspace:GetChildren() do
	if part.Name == "beam" then
		print(beamnum)
		beamnum = round(beamnum + 1)
		beam = "beam"..beamnum
		part.Name = beam
		part:WaitForChild("hi").Touched:Connect(function(hit)
			if debounce then
				debounce = false
				task.wait()
				dskl = dskl + 1
				print(dskl)
				hinum = part.Name
				kamehameha(hit, bonniecheck, freddycheck)
				gold = round(gold + scott)

				task.wait()
				dskl = dskl + 1
				hinum = part.Name
				kamehameha(hit, freddycheck, bonniecheck)
				gold = round(gold - scott)

				task.wait(1)
				debounce = true
			end
		end)
	end
end

Did you try the code I sent you?
Let me know how it works.

Try to not call the same connection on the same part, it can cause bugs and unintended results. Try to encapsulate the code in a single function if you are referring to the same object.

yours wont work because if there isnt a touch ended the character would keep flying to no end or until the debug catches them

Oh my bad, I thought they were both ontouch connect events.

You can disregard what I said earlier.

The code the previous poster sent didnt work for you?

yeah it didnt work. dont worry about fixing it I have a 500 line code that does what I want. I just wanted to optimize it but for some reason it works differently and Idk why

try this :

for _, part in game.Workspace:GetChildren() do
		if part.Name == "beam" then
			print(beamnum)
			beamnum = round(beamnum + 1)
			beam = "beam"..beamnum
			part.Name = beam
            local connection1

			connection1 = part:WaitForChild("hi").Touched:Connect(function(hit)
		        connection1:Disconnect()		
                task.wait()
				dskl = dskl + 1
				print(dskl)
				hinum = part.Name
				kamehameha(hit, bonniecheck, freddycheck)
				gold = round(gold + scott)
			end)
            local connection2
			connection2 = part:WaitForChild("hi").TouchEnded:Connect(function(hit)
		        connection2:Disconnect()		
                task.wait()
				dskl = dskl + 1
				hinum = part.Name
				kamehameha(hit, freddycheck, bonniecheck)
				gold = round(gold - scott)
			end)
		end
	end

sorry I meant to reply sooner but I will test this in a little bit and tell u the results

it doesnt work at all. it basically just disables the script