How would make this script only hit the player once

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)

1 Like
local Debounce = false

script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:WaitForChild("Humanoid")
if Debounce == false then
Debounce = true
humanoid.Health = humanoid.Health - 100
wait(0.5)
Debounce = false
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.

1 Like

its a single player game so that shouldnt effect it

You could try the Disconnect() function:

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)

i would use a simple if statement

if humanoid.Health > 100 then
humanoid.Health -= 100
end

I was about to say this. Simply use an if statement to check whether or not the player had enough health.

Also, you should should stick with 100hp as the max, this way it is more familiar with the players and easier on you as the developer.

im making a double health gamepass so I wanted the player to not get insta killed by the script