Trying to make a script where you deal 5 damage when you step on a part

Here’s the script.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		hit.Parent.Humanoid:TakeDamage(5)
	end		
end)

So, as you know, multiple body parts hit the part whenever you step on it. The problem is whenever you step on the part, multiple body parts hit the part, causing the part to deal 5 more damage every time a body part is touching it. Can you guide me in the right direction(not write out a script for me so, I can learn) to make the script only damage the player once? Thanks in advance!

13 Likes

You can use a debounce table where you insert the player’s name into and remove it after like 3 seconds. And you’re also going to need to check if the player’s name is inside the table everytime the car touches his character, so it doesn’t damage him more than once.

3 Likes

All you have to do is add a debounce.

local Debounce = false
script.Parent.Touched:Connect(function(hit)
if Debounce == false then
    Debounce = true
	     if hit.Parent:FindFirstChild("Humanoid")then
		      hit.Parent.Humanoid:TakeDamage(5)
	      end	
    wait(3)
    Debounce = false
end)
5 Likes

You’re going to have to use something like this:

local hits = {}
part.Touched:Connect(function(hit)
	local character = hit:FindFirstAncestorWhichIsA("Model")
	local humanoid = character and character:FindFirstChildWhichIsA("Humanoid")
	if humanoid and humanoid.Health > 0 and not hits[character] then
		hits[character] = true
		humanoid:TakeDamage(10)
		task.wait(3)
		hits[character] = nil
	end
end)
20 Likes

I’m more of a beginner scripter so this is all confusing. I think I want to learn debounce first to understand it better, then add the suggestions/scripts that you said giggio.

Edit: I’m in the middle of school so sorry if I sound dumb.

3 Likes

I have created one but you have to insert the debounce. Put it under the part that will make you take damage

script.Parent.Touched:Connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5
    end) 
end)

Edit: I found it and modified it from the script of the kill brick from the starter obby map in studios

2 Likes

Lol. I appreciate everyone helping me. I can’t really reply because I’m in school. Thanks! If I don’t get back to you that’s probably why. I’ll look at everything in my free time.

2 Likes
local Hits = {  }
	
	newFire.Touched:Connect(function(Hit)
		if Hit.Parent:FindFirstChild("Humanoid") then
			if Hits[Hit.Parent] then
				return
			end
			Hits[Hit.Parent] = true
			Hit.Parent.Humanoid:TakeDamage(5)
			wait(1)
			Hits[Hit.Parent] = nil
		end
	end)

Do something like this. Easier. And if the objects inside the hit table’s name are the same they won’t get damage.

4 Likes

Try using the following script:

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid")then
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 5
	end
end)
4 Likes

Don’t mean to bump this but This would make an error it would attempt name with nil. How would I fix that?

4 Likes

debounce is easy. it stops a script from running more than once like

local debounce = 1

if debounce == 1 then
    print("hi")
    debounce = 0
end