Why do I need to move inside the part to get damaged?

Hello!

So here I have a big part (50x50x50 studs), and I put this damage script inside it:

local debounce = false
local cooldown = 0.25

part.Touched:Connect(function(hit)
	if hit.Parent.Name == character.Name then return end
		if hit.Parent:FindFirstChild("Humanoid") then
			if table.find(hits, hit.Parent) then return
		else
			if not debounce then
				debounce = true
				hit.Parent:WaitForChild("Humanoid"):TakeDamage(10)
				wait(cooldown)
				debounce = false
			end
		end
	end
end)

The problem, is that I have to actually move while being inside the part to get damaged.
What I want to happen is that I keep getting damaged even while idling inside it.
How can I make that happen? thanks a lot for any help!

4 Likes

You should use game.Workspace:GetPartsInPart(part) to achieve this as Touched isn’t very reliable

while true do
	wait()
	for i,v in pairs(game.Workspace:GetPartsInPart(part)) do
		if hit.Parent.Name == character.Name then continue end
		if hit.Parent:FindFirstChild("Humanoid") then
			if table.find(hits, hit.Parent) then continue
			else
				if not debounce then
					debounce = true
					hit.Parent:WaitForChild("Humanoid"):TakeDamage(10)
					task.wait(cooldown)
					debounce = false
				end
			end
		end
	end
end
2 Likes

I tried your script, but it keeps damaging me even when I leave the part…

Sorry I forgot to mention but put a break after turning off debounce, it’s iterating over every part of the character, damaging the player for those parts

1 Like

Still hitting me when I leave the part. Strange

I tested it and it works completely fine, did you put it in part.Touched? If you do then remove it completely, leave only the while loop

1 Like

yeah I did put it in part.Touched event.

If you want, I can show you the code result :

		part.Touched:Connect(function(hit)
			task.spawn(function()
				while wait() do
					for i, v in pairs(workspace:GetPartsInPart(fireBreath)) do
						if hit.Parent.Name == character.Name then continue end
						if hit.Parent:FindFirstChild("Humanoid") then
							if table.find(hits, hit.Parent) then continue
							else
								if not debounce then
									debounce = true
									combatModule.Hit(player, hit, sounds:WaitForChild("Hit"), values:WaitForChild("DefaultDamage").Value * skillConfig:WaitForChild("DamageMulti").Value)
									task.wait(cooldown)
									debounce = false
									break
								end
							end
						end
					end
				end
			end)
		end)

Remove part.Touched, it will spawn too much loops, the while loop alone works already except if there’s code after it then you probably need spawn

1 Like

Yeah but if I remove that, there’s no more hit variable

		task.spawn(function()
				while task.wait() do -- better to use a timed loop
					for i, hit in pairs(workspace:GetPartsInPart(part)) do
						if hit.Parent.Name == character.Name then continue end
						if hit.Parent:FindFirstChild("Humanoid") then
							if table.find(hits, hit.Parent) then continue
							else
								if not debounce then
									debounce = true
									combatModule.Hit(player, hit, sounds:WaitForChild("Hit"), values:WaitForChild("DefaultDamage").Value * skillConfig:WaitForChild("DamageMulti").Value)
									task.wait(cooldown)
									debounce = false
									break
								end
							end
						end
					end
				end
			end)

Try this, I re-defined the variables in the loop.

1 Like

Same problem, it works but when I stop aiming at the dummy, he is still getting damaged.

What do you mean stop aiming? Can you send me a place file with the entire system so I can test myself?

You said it was massive part that a dummy was supposed to get damaged in

try this:

local debounce = time()
local cooldown = 0.25

task.spawn(function()
	while task.wait() do
		local breakk = true
		for i, hit in pairs(workspace:GetPartsInPart(part)) do
			if hit.Parent.Name == character.Name then
				if hit.Parent:FindFirstChildOfClass("Humanoid") then
					if time() - debounce > cooldown then
						debounce = time()
						combatModule.Hit(player, hit, sounds:WaitForChild("Hit"), values:WaitForChild("DefaultDamage").Value * skillConfig:WaitForChild("DamageMulti").Value)
					end
					breakk = false
				end
			end
		end
		if breakk then
			break
		end
	end
end)
1 Like
local debounce = false
local cooldown = 0.25
local threads = {}
local characterDetected  = {}

-- You can use the more accurate but performance heavy overlapparams or the simple vector within cframe
local function detect()
	
end

-- Your damage function
local function bleed()
	
end

--Start damage
part.Touched:Connect(function(hit)
	--Confirm it's not the same character, you don't want to be stacking damage on the same character
	local findCharacter = characterDetected[character]
	if findCharacter then
		return
	end	
	local findThread = threads[character]
	if findThread then
		task.cancel(findThread)
	end

	thread = task.spawn(bleed())
end)

--End damage
part.TouchEnded:Connect(function(hit))
	-- You will need to do the same when character dies or player leaves the server; to prevent memory leak.
	local findCharacter = characterDetected[character]
	if findCharacter then
		characterDetected[character] = nil
	end	
	local findThread = threads[character]
	if findThread then
		task.cancel(findThread)
	end
end)

OverlapParams:

Position within a part.

1 Like