Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a part only touch once a humanoid?

So I've made a bullet, that deals damage, but I've noticed that it kills the humaoid (zombies) instantly. What do I want to achieve: -When the bullet touches a humanoid it only damages it once. -And don't delete the bullet, when it touches a humanoid, so once bullet can touch more humanoids. my script:

local bullet = script.Parent
local function onTouch(partOther)
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")

	if not humanOther then return end
	if humanOther.Parent == bullet then return end
	if game.Players:GetPlayerFromCharacter(partOther.Parent) then return end

	humanOther:TakeDamage(25)
end

script.Parent.Touched:Connect(onTouch)

Thanks for reading!

1 answer

✓ Accepted answer

The .Touched event fires multiple times when it touches a single object, so it run the onTouch event multiple times and subsequently damage the zombie multiple times, killing it.

This can be fixed by storing a list of all zombies damaged, so that we can check if we have already damaged said zombie or not.

local damaged = {}

local function onTouch(partOther)

	local humanOther = partOther.Parent:FindFirstChild("Humanoid")

	if not humanOther then return end
	if humanOther.Parent == bullet then return end
	if game.Players:GetPlayerFromCharacter(partOther.Parent) then return end
	
	if table.find(damaged, humanOther) then return end

	table.insert(damaged, humanOther)
	
	humanOther:TakeDamage(25)

end

4 comments

0

Thanks for you reply, but now it says "Workspace.Bullet.BulletDamageScript:15: attempt to call a nil value " line 15 is "table.add(damaged, humanOther)", how can I fix this?

0

oopsies its table.insert(damaged, humanOther) sorry

0

Yep, it's working! Thank you!

0

Yep, it's working! Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.