I made this script that does 100 damage to the player how would I make it so it won’t hit the player twice since they have 200 health
code
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA(“Humanoid”)
if humanoid then
humanoid.Health = humanoid.Health-100
end
end)
A potential issue with this is that if one person gets hit, no one else will be able to be hit by it until that 0.5 seconds passes. I’d recommend using tables:
local PlayerDamages = {}
script.Parent.Touched:Connect(function(hit)
local Model = hit:FindFirstAncestorOfClass("Model")
local Humanoid = Model and Model:FindFirstChildOfClass("Humanoid")
if Humanoid and not PlayerDamages[Humanoid] then
PlayerDamages[Humanoid] = true
Humanoid:TakeDamage(100) -- Respects forcefields
task.delay(0.5,function()
PlayerDamage[Humanoid] = nil
end)
end
end)
That way, it won’t allow one person to get hit and ignore the rest.
local event
event = script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health-100
event:Disconnect()
end
end)