You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to Prevent this loop from executing twice which is dealing more damage than should and essentially applying the logic twice
What is the issue? Include screenshots / videos if possible!
As stated before in my hit detection logic the problem is the loop is executing twice I want it to execute Once
local hitEnemies = {}
for _, object in ipairs(touchedObjects) do
if object:IsA("BasePart") and not object:IsDescendantOf(character) then
if table.find(hitEnemies,object) then continue end
table.insert(hitEnemies,object.Parent)
local enemyCharModel = object:FindFirstAncestorOfClass("Model")
local enemyHumanoid = enemyCharModel and enemyCharModel:FindFirstChildOfClass("Humanoid")
local enemyAnimator = enemyHumanoid:FindFirstChildOfClass("Animator")
if enemyHumanoid and not hitEnemies[enemyCharModel] then
print(hitEnemies[1])
LoadHitReactionTracks(enemyAnimator)
randomHitTrack(enemyAnimator)
enemyHumanoid:TakeDamage(Toolstat.Damage)
StunMod.Stun(enemyHumanoid, Toolstat.HitStunDuration)
hitEnemies[enemyCharModel] = true
print("ha")
print("Touched instance:", object:GetFullName())
end
end
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’m not sure what methods to try, I’ve tried experimenting with return statements and break as well I couldn’t find a problem on forums relative to my issue
Best way I can think of to approach this is by implementing a Debounce. It’s basically a variable you’ll define as false at the start of your script. When your loop runs, check the value of that variable. If it’s true, then return. If false, then proceed. Immediately after that, set the variable to true. This will ensure that the loop only runs when you want it to. At the end of your loop, you can add a delay if necessary and set the variable to false so that the loop can run again.
It should look something like this:
Just for the sake of the example, I’ll be using a click detector event.
local debounce = false
ClickDetector.MouseClick:Connect(function()
if debounce then return end -- returns the function if debounce is true
debounce = true
-- super cool code stuff
task.wait(3) -- delay before the function can run again
debounce = false
end)
I think your problem might be that you’re adding the first parent of the hit part to the table, rather than the enemy model. This means that if you were to hit the part of an accessory or a part inside a group inside the model, then the actual enemy model isn’t in the debounce table, leading to additional damage.
Alongside this, there are a few changes you can make to the script for efficiency, mostly just in checking if the object and an enemy has already been hit.
local hitEnemies = {}
--you don't really need to check for things in a certain order for hitboxes, if that's what this script is for.
--so you just need pairs instead of ipairs
for _,object in pairs(touchedObjects) do
if object:IsA("BasePart") and not object:IsDescendantOf(character) then
local enemyCharModel = object:FindFirstAncestorOfClass("Model") --honestly this is good, i've been manually checking for the parent so its nice to see that there is a more convenient method
local enemyHumanoid = enemyCharModel:FindFirstChildOfClass("Humanoid")
if enemyHumanoid and not table.find(hitEnemies, enemyCharModel) then --you only need to do one check to see if the enemy has been hit before.
table.insert(hitEnemies, enemyCharModel)
--from there it looks good, it was mostly just the table checking which i was concerned about.