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

How to make this only damage the same player once?

Asked by
sonuka 24
6 years ago

How can I make this to only damage the same player once?

x.Touched:connect(function(hit)
if x.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid").Health > 0 then 
x.Parent.Humanoid.Health = hit.Parent.Humanoid.Health-10
0
Your variable "x" is referencing a BasePart which I assume is a tool's Handle. Yet on the third line, were you take 10 hp from a humanoid's health pool, you are damaging the owner of the tool rather than the player who got hit by it. Idk if this is intended, could you specify it please? Le_Teapots 913 — 6y
0
x is a block in workspace sonuka 24 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
local PlayersHitted = {}
local x = script.Parent -- Assuming your script is inside the part, change otherwise

x.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Humanoid").Health > 0 then

        for i,tableValue in pairs(PlayersHitted) do
            if tableValue == hit.Parent.Name then return end
        end
        table.insert(PlayersHitted, hit.Parent.Name)
        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health-10
    end
end)

Tested it on studio, it works.

What we are doing here is store the name of every player who touches the brick, therefor whenever the brick gets touched by the same player again it will first check for the stored names and if the player's name matches any of the stored names (which, since its the same player, it will) then the brick doesnt deal any damage.

Best regards!

1
Thanks! sonuka 24 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
local lastPersonHit = nil

x.Touched:Connect(function(hit)
if x.Parent:FindFirstChild('Humanoid') and hit.Parent:FindFirstChild('Humanoid').Health > 0 and lastPersonHit ~= hit.Parent.Name then
lastPersonHit = hit.Parent.Name
x.Parent.Humanoid.Health:TakeDamage(10)
end
end)

0
Uhm this makes it so that x only damages one player and stops damaging others, I want it to damage the same player only once sonuka 24 — 6y

Answer this question