Touch event acting weird

so im trying to make a humanoid take damage until touch end how ever the TouchEnded triggers while the player is still inside the part, im using a mesh part as hitbox tbh i don’t know why its doing that, tried using a repeat event since it semmed more fit to this context but don’t really know how to put the until in form since repeat unit Part.TouchEnded shouldn’t distinguish what part triggered the event so i made this, also i’dd like it being able to damage everyone in the hitbox

how it currently behaves : https://gyazo.com/c80cb9f8da299c2389e48c83bac6483a

Hitbox.Touched:Connect(function(hit)
		if hit:IsA("Part") and hit.Name == "HumanoidRootPart" and hit ~= player.Character.HumanoidRootPart then
			local humanoid = hit.Parent.Humanoid

			humanoid:TakeDamage(10)

			
			table.insert(HitTable, hit)
		end
	end)
	```
1 Like

Touched event has always been a bit wonky as it only works if you keep moving inside the touch part so I recommend you do something like this instead:

local workspace = game:GetService"Workspace"

local hitBox = -- Where the HitBox is located

local debounce = {}

local wrap = coroutine.wrap

local connection -- Disconnect this connection if you want the loop to stop running
connection = game:GetService"RunService".PostSimulation:Connect(function()
	for _, basePart in workspace:GetPartsInPart(hitBox) do
		if not basePart.Parent then continue end

		local humanoid = basePart.Parent:FindFirstChildWhichIsA"Humanoid"

		if humanoid and not debounce[humanoid] then
			wrap(function()
				debounce[humanoid] = true
				humanoid:TakeDamage(10)
				task.wait() -- Enter desired wait duration here
				debounce[humanoid] = nil
			end)()
		end
	end
end)
4 Likes

my god i didn’t expect to get such help so fast thanks dude its works perfectly i really need to learn Run service it looks like it can do everything

2 Likes

I had to do it before that’s why :wink:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.