Damaging part not working

So, i’ve been experiencing a problem that you may consider easy one. But it has been a thorn in my side and i don’t know how to fix it. So I have a part that gives damage and everytime a player touches it it gives him damage and when he’s no longer touching the part he won’t get damage anymore. I have tried all the things possible but none of them don’t work. I’ve tried functions, while loops and all this kind of stuff. But still no result. Any possible solution given by you will be welcomed.

So you need to use touched and touchended to get when the player is touching or no longer touching, then its a simple while loop to do the damage while they are

1 Like

The code will probably look like this ( only a fraction of the code are shown because you
( referring to OP ) need to figure what the rest code structure will look like. ) :

while true do
	if Touching ~= false then
		-- Damage Humanoid or whatever
		task.wait(.25) -- Cooldown
	end
end

Asuming touching here is a bool yes that should work, only most people would do it as a while true as its easier to follow your code should you come back to edit it

local touching = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		touching = true
		while touching == true do
			wait(5)
			hit.Parent:FindFirstChild("Humanoid").Health -= 10
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		touching = false
	end
end)

So, i don’t know what i’ve done wrong but the loop doesn’t work as expected. And that wait that i’ve put in the while loop doesn’t seem to follow the amount of seconds that i gave him. Like i touch it and then get away and the damage begins really fast, like it’s not a 5 second delay between each damage. I clearly don’t know what i have done wrong.

What your code is doing is creating another while loop every time the Touched event is fired. Instead, I recommend implementing a debounce to act as a cooldown between damaging players. Something like this:

local canDamage = true
script.Parent.Touched:Connect(function(hit)
    if canDamage and hit.Parent:FindFirstChildOfClass("Humanoid") then
        canDamage = false
        hit.Parent:FindFirstChildOfClass("Humanoid").Health -= 10
        task.wait(5)
        canDamage = true
    end
end)
1 Like

You added the loop inside of the “Touched” which will fire when your part are touched ( I think ).

Ok please NEVER use a while loop in a function like that it unless it is being called once and will actually stop would cause a memory leak and that’s not good. Also it would break if multiple people touched the part. So try this

local lasttouched = {}

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		lasttouched[hit.Parent.Name] = hit.Parent.Humanoid
		
	end
end)


script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		lasttouched[hit.Parent.Name] = nil
	end
end)

 while true do
	 for i,v in pairs(lasttouched) do
			v:TakeDamage(10)
	 end
  task.wait(0.1)
 end

Your code works perfectly but there’s one issue. Like, when I’m walking on the part it gives me damage, but when i’m just standing on it, it doesn’t give me that 10 damage.

You made a mistake right here, it should be “nil” instead of “nill”

Ye sorry the devforum codie bit can get confusing

It gives me the error Humanoid is not a valid member of MeshPart “Workspace.Qapandt.LeftFoot”, and the same to every part of the character.

Oops forgot about the .Parent bit fixed it now :slight_smile:

It’s because the code is trying to get humanoid from hit/touched part

1 Like

Um, the code doesn’t seem to reach the for loop because it isn’t giving me any damage.
robloxapp-20230421-0958274.wmv (747.6 KB)

Ye ik why it because i put in the check lasttouched[1] and there won’t be that because it sets it to be a player’s name so just remove that if statement and it should all work :slight_smile:

It works perfectly but sometimes the part has some weird behavior like when i’m standing on it doesn’t give me any damage but when I get away from it and the comeback again. It works fine. But these things are kinda rare, and overall it seems to work.

1 Like

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