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:
01 | local deb = false |
02 | Part.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 |
14 | 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.
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 |
08 | local Part = script.Parent |
09 |
10 | --Variables |
11 |
12 | local COOL_DOWN = 5 |
13 |
14 | --//Functions |
15 |
Hope this helps, if you have any questions, leave a comment!
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
01 | local deb = false |
02 | Part.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 | if 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 |
15 | end ) |