Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this only damage the same player once?

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

2 comments

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?

0

x is a block in workspace

2 answers

✓ Accepted answer
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 comment

1

Thanks!

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)

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.