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

How would I make a .Touched Event hit multiple players at once?

Asked by 4 years ago

I'm trying to make it where once the players are hit by a part it damages them, but also uses debounce so the damage only fires once for each player.

What I have so far:

local deb = false
Part.Touched:Connect(function(hit)
    if not deb then
        if hit.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
            deb = true
            local targetPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
            local targetCharacter = targetPlayer.Character
            local targetHumanoid = targetCharacter.Humanoid

            targetHumanoid:TakeDamage(25)
        end
        deb = false
    end
end)

Note: This isn't the real script just an example. The problem with this example script is that it would only hit the first player, damage that player, then hit the other, but it wouldn't hit them both at once.

1
Ah, so you're using 1 part to damage multiple players, but you don't want it to hurt them more than once? Psudar 882 — 4y
1
^ Yes, I've read somewhere that you're supposed to use a table whilelist to check if the player has been hit before, but I have no clue how that would work. KardashevScale 110 — 4y
0
YO you are gonna need Remote Functions ExHydraboy 30 — 4y

2 answers

Log in to vote
1
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

Okay, so I will explain how I would do it. You mentioned tables in the comments, and I think thats a very great approach. You're off to a good start.

First, I would use the part.Touched event. Then, I would want to check if they are a valid character FindFirstChild("Humanoid") and then check if they're already in the table or not. I wrote a function that basically just loops through a given table and returns true if the value inserted in the argument is in that table. If the if-statement passes, that means that the player who touched it ISNT in the table, so we accordingly add them. Then, we do damage, wait for a cooldown, and then remove them. Just like a debounce.

--Psudar
--Sep 1 2019
--ServerScript

--//Instances

--script is parented to the part
local Part = script.Parent

--Variables

local COOL_DOWN = 5

--//Functions

--this func checks if a value is in a table
local function FindEntry(tbl, val) 
    for iteration, value in pairs (tbl) do
        if value == val then
            --if found, return true
            return true 
        end
    end
end

--this is the table we're gonna store players in
local TouchedPlayers = {} 
--t == touched or hit, just shortened it
local function onTouched(t)
    --check if its a player and if they're not in the table
    if t.Parent:FindFirstChild("Humanoid") and not FindEntry(TouchedPlayers, t.Parent) then 
        --if they aren't, put the character in the table
        --add them into the table at the most recent entry, +1
        table.insert(TouchedPlayers, #TouchedPlayers + 1, t.Parent) 
        --do damage to the player
        t.Parent.Humanoid:TakeDamage(25) 
        wait(COOL_DOWN)
        --removes the most recent entry (would be our player)
        table.remove(TouchedPlayers, #TouchedPlayers)
    end
end

--//Connections

--connect function on touched 
Part.Touched:Connect(onTouched)

Hope this helps, if you have any questions, leave a comment!

1
Thanks man, just got done testing it. This was exactly what I was looking for! KardashevScale 110 — 4y
1
Np! :D Psudar 882 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

its really easy tbh

basically it changes the players name to hit and if the player got hit then the script says no dont hit agian


local deb = false Part.Touched:Connect(function(hit) if not deb then if hit.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then deb = true local targetPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) local targetCharacter = targetPlayer.Character if not targetCharacter.Name == "hit" then local targetHumanoid = targetCharacter.Humanoid target.Character.Name = "hit" targetHumanoid:TakeDamage(25) end deb = false end end)
0
That wont work if 2 people touch it at the same time. You missed the point of his question, mate. Psudar 882 — 4y

Answer this question