Code issues - Humanoid.Health -= 20

Hello! what’s wrong in my code? how could i fix that? :frowning:

local v = script.Parent
local t = false

v.Touched:Connect(function(player)
	t = false
	player.Parent:FindFirstChild("Humanoid").Health -= 20
	t = true
	wait(3)
end)

I need to make the player -20 hp each time (every 3 seconds) that the part is touched

Why do you need the t variable and why do you need the wait(3)?

that’s his debounce i’m pretty sure

with the t I want to do a debounce and do it every touched 3 seconds

But he doesn’t wait and why would you set your debounce to false first, then to true?

because it’s set to true after the humanoids health is changed

local v = script.Parent
local t = true

v.Touched:Connect(function(player)
    if t then
	t = false
      if player.Parent:FindFirstChild("Humanoid") then
	     player.Parent.Humanoid:TakeDamage(20)
       end
	wait(3)
    t = true
   end
end)
1 Like

Thank you very much! that i needed to do :smiley:

It seems you’re not checking if it’s a player so the script errors.

Here’s the fixed version

local v = script.Parent
local t = true

v.Touched:Connect(function(hit)
if t==true then
t= falsr
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health -= 20
wait(3)
t=true
end
end
end)

Oops just realised someone else sent it. Lol

1 Like
local v = script.Parent
local t = false

v.Touched:Connect(function(player)
    if not t then
            if player.Parent:FindFirstChildWhichIsA("Humanoid") then
                player.Parent:FindFirstChildWhichIsA("Humanoid").Health -= 20
                t = true
                wait(3)
                t = false
        end
    end
end)
1 Like

(not sure if this will work it’s just what works for leaderstats int values so maybe it works)
try

player.Parent:FindFirstChild(“Humanoid”).Health = player.Parent:FindFirstChild(“Humanoid”).Health - 20

or if that doesn’t work just swap out -= 20 to - 20, not sure if anyone will work, just a guess

No -= always works too.

Why does Roblox hate short replies?

2 Likes