Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i make a touch function that hits multiple people, but only once?

Asked by
rexpex 45
7 years ago

Im making an aura attack move, but its only able to attack 1 person at a time. Here is my script

Strong.Touched:connect(function(hit)

if not debounce then return end
debounce = false
local dmg = math.floor(script.Parent:FindFirstChild("Charging").Value / 1.55 + baseAtk + 1)
local opponent = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
        if opponent and opponent ~= player.Character.Humanoid then
           opponent:TakeDamage(dmg)

wait(0.23)
debounce = true 
end)

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

Your usage of a debounce variable is stopping it from running on more than one player at a time. Since, I assume, you have the debounce there to stop it from damaging the same player more than once, you should use a table for that instead:

local debounceTab = {};
Strong.Touched:Connect(function(hit)
    local opponent = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid");
    if opponent and opponent ~= player.Character.Humanoid then
        if debounceTab[opponent] then return end;
        debounceTab[opponent] = true;
        local dmg = math.floor(script.Parent:FindFirstChild("Charging").Value / 1.55 + baseAtk + 1)
        opponent:TakeDamage(dmg)
        wait(0.23)
        debounceTab[opponent] = nil;
    end
end)

Also, I noticed you had an end missing in your code. I'm not sure how it didn't error. Make sure you correctly indent your code!
Hope this helped.

Ad

Answer this question