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.
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.
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.
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
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
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)
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!