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.
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!
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)