Touched statement ignoring debounce

I have a script that when you touch a part, you should get slow and get damaged, but it is running four times. I have no clue why because I have a debounce. This is the script:

local db = false
script.Parent.Touched:Connect(function(otherpart)
	if otherpart.Parent:FindFirstChild("Humanoid") and db == false then
			db = true
			otherpart.Parent.Humanoid:TakeDamage(10)
				otherpart.Parent.Humanoid = otherpart.Parent.Humanoid - 15
			wait(1)
				otherpart.Parent.Humanoid = otherpart.Parent.Humanoid + 15
				db = false
			end
end)

Try putting the debounce in its own if statement.

Are you connecting it 4 times?

Anyways, you seem to be subtracting the humanoid itself, and not the walkspeed. I’ve fixed it in this version:

local db = false
script.Parent.Touched:Connect(function(otherpart)
	local humanoid = otherpart.Parent:FindFirstChild("Humanoid")
	if humanoid and not db then
		db = true
		humanoid:TakeDamage(10)
		humanoid.WalkSpeed -= 15
		task.wait(1)
		humanoid.WalkSpeed += 15
		db = false
	end
end)

Thanks for the walkspeed fix, but it still runs 4 times, what do I do?

It seems like you’re connecting the event 4 times. Make sure that it’s not due to that.

What do you mean? Its a touch event. It only fires when you touch it.

One connection to one event will run once per event. Four connections to the same event will run once per event, but add up to four in total.

For example, this will run the same function four times:

local function onHit(hit)
   print("hit: "..hit.Name)
end

brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
brick.Touched:Connect(onHit)
script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)

local humanoid = otherpart.Parent:FindFirstChild("Humanoid")

    if player and humanoid then
        if not player:GetAttribute(“speedDB”) then
		player:SetAttribute(“speedDB”, true)
		humanoid:TakeDamage(10)
		humanoid.WalkSpeed -= 15
		task.wait(1)
		humanoid.WalkSpeed += 15
		player:SetAttribute(“speedDB”, false)
	end
end)

Try this I haven’t tested it, I’m on phone rn

Could you print the “otherpart”? Maybe it has something to do with these.

It shouldn’t but I guess if they wanted to.


script.Parent.Touched:Connect(function(otherpart)
	local player = game.Players:GetPlayerFromCharacter(otherpart.Parent)

local humanoid = otherpart.Parent:FindFirstChild("Humanoid")

    if player and humanoid then
        if not player:GetAttribute(“speedDB”) then
		player:SetAttribute(“speedDB”, true)
		local originalSpeed = humanoid.WalkSpeed
humanoid:TakeDamage(10)
		humanoid.WalkSpeed -= 15
		task.wait(1)
		humanoid.WalkSpeed = originalSpeed
		player:SetAttribute(“speedDB”, false)
	end
end)

Prob best if u do this as well

I already have the speed working, its just that it runs four times.

I dont do it four times though.

Have you tried my script yet, is it still occurring with mine?

There isn’t a debounce in your script, it is basically just my script but with the player having a speed attribute and it doesn’t have a debounce.

There is a denounce in mine. It just does it per player so if someone goes after another it will still speed the second player

Can we see the full script, or the script that you’re using to connect events? Are you modifying db anywhere else? Maybe a place file as well?

I’m still fairly certain you’re connecting an event four times, either through a loop or through the creation of multiple scripts. You should find the source of the problem first, as that’ll cause a greater performance issue in the future. Using UhTrue’s script will work right now, but you’ll be evading the real problem.

that is the full script that I put at the beginning. There is nothing else in ti.

Are you cloning the script and placing it underneath anything?

I highly recommend you upload a place file with just the script and the part you’re trying to use. I’ll be more helpful with one.