Touched and TouchEnded glitched

Hello. I am making wind blow ability, that should slowly push back and deal some damage everyone that touches its radius, however i dont know why, but Touched/TouchEnded function keeps spamming when I add Velocity to character that touched that radius. Can someone help me?

2 Likes

You need to add a cooldown to the touched function to avoid the spam, the touched event is very fast, it detect every micro movements, so when you touch it only one time the function can be runned like +10 times.

Something like this:

local Part = script.Parent
local Ready = true

Part.Touched:Connect(function()
	if Ready == true then
		Ready = false
		--code
		task.wait(1)
		Ready = true
	end
end)
3 Likes

is it possible to make it like just once? like only time touched

1 Like

Yeah, should it be touched one time in total, or one time per player ?
If one time in total, it is the exact same thing, just removing the last line

local Part = script.Parent
local Ready = true

Part.Touched:Connect(function()
	if Ready == true then
		Ready = false
		--code
	end
end)
1 Like

Overall it should be one time per player

You can just set CanCollide to false, instead of adding debounce, as it will also prevent Touched and TouchEnded event from firing again.

1 Like

it is alr. I dont know why the touchended keeps spamming. It does all the time when player that touches radius is moving by velocity

Alright so you can do something like this:

local PlayerService = game:GetService("Players")
local Players = {}

local Part = script.Parent

Part.Touched:Connect(function(Hit)
	local Character = Hit.Parent
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
	
	if Humanoid and PlayerService:FindFirstChild(Character.Name) and not Players[Character.Name] then
		Players[Character.Name] = true
		--code
	end
end)
1 Like

It were just another method to solve your problem, @Crygen54 did everything right but i were just suggesting so.

its not only touched function that keeps spamming. I would say that touchended keeps spamming instead of touched function

That’s the exact same method, but in another way, here is a code you can try on a part, it allow all player to touch the part only one time (including Touched and TouchEnded).

local PlayerService = game:GetService("Players")

local Part = script.Parent
local Players = {}

Part.Touched:Connect(function(Hit)
	local Character = Hit.Parent
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

	if Humanoid and PlayerService:FindFirstChild(Character.Name) then
		if not Players[Character.Name] then
			Players[Character.Name] = {false, false}
		end
		
		if Players[Character.Name][1] == false then
			Players[Character.Name][1] = true
			print(Character.Name.. "Touched")
		end
	end
end)

Part.TouchEnded:Connect(function(Hit)
	local Character = Hit.Parent
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

	if Humanoid and PlayerService:FindFirstChild(Character.Name) then
		if Players[Character.Name] and Players[Character.Name][1] == true and Players[Character.Name][2] == false then
			Players[Character.Name][2] = true
			print(Character.Name.. "TouchEnded")
		end
	end
end)

Also, you can reset all players touch like this if needed

table.clear(Players)

What could possibily help you with radiuses and “touched” are Regions (Region3). You could make a region and check if a player is inside (very simple and yet efficient), then give them damage.

Reference: Region3 | Documentation - Roblox Creator Hub

Tutorial: https://www.youtube.com/watch?app=desktop&v=kRuSWSe9faY

Hope this helps!

You may be better off adding the TouchEnded function to the end of the Touched function.

No, doing function inside function is messy and disorganized, it’s also unnecessary and makes things a lot more difficult to read.

1 Like

And it will also only try to do end touch after an actual touch. Myself I’ve had so many problems with endtouch I quit using it …

1 Like

Yeah, that’s the point of TouchEnded event, it should be fired only after a Touched event