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 5 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:

01local deb = false
02Part.Touched:Connect(function(hit)
03    if not deb then
04        if hit.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
05            deb = true
06            local targetPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
07            local targetCharacter = targetPlayer.Character
08            local targetHumanoid = targetCharacter.Humanoid
09 
10            targetHumanoid:TakeDamage(25)
11        end
12        deb = false
13    end
14end)

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 — 5y
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 — 5y
0
YO you are gonna need Remote Functions ExHydraboy 30 — 5y

2 answers

Log in to vote
1
Answered by
Psudar 882 Moderation Voter
5 years ago
Edited 5 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.

01--Psudar
02--Sep 1 2019
03--ServerScript
04 
05--//Instances
06 
07--script is parented to the part
08local Part = script.Parent
09 
10--Variables
11 
12local COOL_DOWN = 5
13 
14--//Functions
15 
View all 46 lines...

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 — 5y
1
Np! :D Psudar 882 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 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

01local deb = false
02Part.Touched:Connect(function(hit)
03    if not deb then
04        if hit.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
05            deb = true
06            local targetPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
07            local targetCharacter = targetPlayer.Character
08if not targetCharacter.Name == "hit" then
09            local targetHumanoid = targetCharacter.Humanoid
10            target.Character.Name = "hit"
11            targetHumanoid:TakeDamage(25)
12        end
13        deb = false
14    end
15end)
0
That wont work if 2 people touch it at the same time. You missed the point of his question, mate. Psudar 882 — 5y

Answer this question