How can I damage characters only once using my hitbox?

I’m trying to make a hitbox function which when touched, will damage a player once. However, i’ve found that if the other player so as much moves inside the hitbox, the function will activate again and proceed to hurt them once again.

I was wondering if there was a way to make this only fire once whenever the hitbox touches the character(s) for the first time?

local function getHitted(part, parentalplayer)
	if part:IsDescendantOf(parentalplayer.Character) then return end --the parental player is the one that created the hitbox, as such, we don't want them to get hit by their own hitbox.
	if part.name ~= "Head" then return end
	local character = part.Parent

	--[[if getHitted() ~= nil or character then
		print("not again")
		character.Humanoid.Health = character.Humanoid.Health - 10
	end
	return character]]
-- the comments above were my latest attempts at this issue. I thought returns set a value to my functions that I could call apon later to see whether the character was the same, but it appears this doesn't work, and also comes with resetting the variables into some odd things.
end

getHit.OnServerEvent:Connect(function(player, cframe)
	local hitbox = getHitter:Clone()
	hitbox.CFrame = cframe
	hitbox.Parent = workspace
	hitbox.Touched:Connect(function(part) 
		getHitted(part, player)
	end)
	wait(5)
	hitbox:Destroy()
end)

The ideal outcome for script is for it to be able to hit multiple players at once.

2 Likes

use :Once instead of :Connect

	hitbox.Touched:Once(function(part) 
		getHitted(part, player)
	end)
1 Like

Sadly, this does not work in my case. It’s a helpful addition, but in the ideal case, I want it to be able to hit multiple people at once.
Alongside this, I currently have a limitation which makes it only react to heads. This makes this inconsistent, as if you touch the hitbox with any other part the attempt is made irrelevant. tho i will say that I could bypass this through editing the code.

2 Likes

You could also add a debounce.

db = false
getHit.OnServerEvent:Connect(function(player, cframe)
local hitbox = getHitter:Clone()
hitbox.CFrame = cframe
hitbox.Parent = workspace
hitbox.Touched:Connect(function(part)
If db = false then
getHitted(part, player)
db = true
Task.wait(1)
db = false
End
end)
wait(5)
hitbox:Destroy()
end)

Sorry for very weird formating and capslock, im on Tablet rn.

2 Likes

Ey, thanks for the input, but it faces the same problems as the ones I noted for czo’s reply.

2 Likes

So Im Not currently able to write the Script, but my next idea would be to add the debounce but only when the plrname is in a Tablet. So save all hitted Players in a Tablet and then sth Like If Not tablet.find then damage him, add to the table, and delete the Player after theyre damage cooldown Out of the table. Maybe that helps you.
Btw, Tablet = table

2 Likes

you change the db thing to a bool value, example

if not hit.Parent:FindFirstChild("Hitted") then -- check if there is no bool value with the name "Hitted"
	local Hitted = Instance.new("BoolValue",hit.Parent) -- making a new bool value
	Hitted.Name = "Hitted" -- naming the bool value
	game.Debris:AddItem(Hitted,1) -- Change 1 to any second you want, this will destroy the bool value after 1 second or any number you change to (game.Debris is better than Destroy() )
	
end
2 Likes

Here is a better script:


getHit.OnServerEvent:Connect(function(player, cframe)
	local hitbox = getHitter:Clone()
	hitbox.CFrame = cframe
	hitbox.Parent = workspace
	hitbox.Touched:Connect(function(part) 
		if not part.Parent:FindFirstChild("Hitted") then
			local Hitted = Instance.new("BoolValue",part.Parent)
			Hitted.Name = "Hitted"
			getHitted(part, player)
			game.Debris:AddItem(Hitted,1)
			end
	end)
	game.Debris:AddItem(hitbox,5)
end)

3 Likes

I’d avoid using .Touched and use :GetPartsBoundsInBox() instead, or maybe Magnitude if we want things to be really simple.

Anyways, create a table that will store everyone attacked by the hitbox. When the hit event fires, check to see if the new target is in the hit table. It would looks something like this:

local HitNoobs = {}

function getHitted(newHit,parentPlayer)
	if not table.find(HitNoobs,newHit.Parent) then
		--Damage thingies here.
		table.insert(HitNoobs,newHit.Parent)
	end
end

--After an attack, clear the HitNoobs table with table.clear(HitNoobs)
1 Like

Thanks for the tip, i’ll look into :GetPartsBoundsInBox() later.

1 Like

Proper thanks pal, I can even replace the value with blood particles, as the value part of the boolvalue is not used whatsoever. This is of great help, as I can deal with 2 birds using only one stone!

yeah if there is something adds when hitting a target, you can always replace the bool value.

1 Like

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