DMGPart wont damage when you dont move

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!
    Making DMGPart do damage if you stay still on it

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    idk how
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local waitTime = 0.2
local Debounce = false

local function Touched(hit)
	if not Debounce then
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		Debounce = true
		local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid") 		
		Humanoid:TakeDamage(10)
		wait(waitTime)
		Debounce = false
	end
end

script.Parent.Touched:Connect(Touched)
1 Like

try this:

local waitTime = 0.2
local Debounce = false
local Char = {}

local function Touched(hit)
	if not Debounce then
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		Debounce = true
		hit.Parent:SetAttribute("Damage", true)
		table.insert(Char, hit.Parent)
		wait(waitTime)
		Debounce = false
	end
end

local function TouchEnded(hit)
	if not Debounce then
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		Debounce = true
		hit.Parent:SetAttribute("Damage", false)
		table.remove(Char, table.find(hit.Parent))
		wait(waitTime)
		Debounce = false
	end
end

while task.wait(waitTime) do
	if table.getn(Char) > 0 then
		for _, Plr in pairs(Char) do
			if Plr:GetAttribute("Damage") == true then
				Plr.Humanoid:TakeDamage(10)
			end
		end
	end
end

script.Parent.Touched:Connect(Touched)
script.Parent.TouchEnded:Connect(TouchEnded)
1 Like

I tried this, but it doesnt damage me now, idk why

is it giving any errors? (actually i spelt something wrong, copy it again)

Nope, there are no errors in the output, and it doesnt do any damage again lol

@vParkuu You have the right idea, just a little mistake. The loop keeps the connection of the touch events from running since the thread stays stuck in the loop forever. we can just move the connections above the loop.

So this would be the updated script from Park.

local waitTime = 0.2
local Debounce = false
local Char = {}

local function Touched(hit)
	if not Debounce then
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		Debounce = true
		hit.Parent:SetAttribute("Damage", true)
		table.insert(Char, hit.Parent)
		wait(waitTime)
		Debounce = false
	end
end

local function TouchEnded(hit)
	if not Debounce then
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		Debounce = true
		hit.Parent:SetAttribute("Damage", false)
		table.remove(Char, table.find(hit.Parent))
		wait(waitTime)
		Debounce = false
	end
end

script.Parent.Touched:Connect(Touched)
script.Parent.TouchEnded:Connect(TouchEnded)

while task.wait(waitTime) do
	if table.getn(Char) > 0 then
		for _, Plr in pairs(Char) do
			if Plr:GetAttribute("Damage") == true then
				Plr.Humanoid:TakeDamage(10)
			end
		end
	end
end

I wasn’t able to test it so hopefully that’s all that it was!

Workspace.Part.Damage:21: invalid argument #1 to ‘find’ (table expected, got Instance)

Replace line 21 with this

table.remove(Char, table.find(Char, hit.Parent))

The issue was that the table Char wasn’t included as the first parameter for the table.find function.

sometimes it still continues to damage me, even when i was off the platform

Try using WorldRoot:GetPartsInPart
when the touch begins just do a loop of getParts until there are no parts in it then stop the loop
ex.

local conn
conn = part.Touched:Connect(function(t)
if t.playerstuff then -- check if player
conn:Disconnect()
while #part:GetPartsInPart() > 0 do
parts = part:GetPartsInPart()
for i,v in pairs(parts) do
if v --check if player then
--do some damage
end
end
end
end
end)
local Game = game
local Run = Game:GetService("RunService")
local Workspace = workspace
local Part = Workspace.Part --Reference to part that deals damage.

local Humanoids = {}

local function OnAncestryChanged(Child, Parent)
	if Parent then return end
	Humanoids[Child] = nil
end

local function OnTouched(TouchedPart)
	local TouchedModel = TouchedPart:FindFirstAncestorOfClass("Model")
	if not TouchedModel then return end
	local TouchedHumanoid = TouchedModel:FindFirstChildOfClass("Humanoid")
	if not TouchedHumanoid or (TouchedHumanoid and TouchedHumanoid.Health <= 0) then return end
	if Humanoids[TouchedHumanoid] then return end
	TouchedModel.AncestryChanged:Connect(OnAncestryChanged)
	Humanoids[TouchedHumanoid] = 0
	while true do
		local Parts = Part:GetTouchingParts()
		local State = false
		for _, Part in ipairs(Parts) do
			local Model = Part:FindFirstAncestorOfClass("Model")
			if not Model then continue end
			if Model == TouchedModel then State = true break end
		end
		if not State then Humanoids[TouchedHumanoid] = nil break end
		local CurrentTime = Run.Stepped:Wait()
		if (CurrentTime - Humanoids[TouchedHumanoid]) < 0.2 then continue end
		Humanoids[TouchedHumanoid] = CurrentTime
		TouchedHumanoid:TakeDamage(10)
	end
end

Part.Touched:Connect(OnTouched)

This is working well on my end. I wrote the script from scratch, so you may need to make a few edits for it to be compatible with your game.