Humanoid health stays the same?

I made this script inside of the handle of my tool to take away humanoid health but it is not seeming to work as intended on the “dummies”.

function onTouch(hit)

	script.Disabled = true

	local humanoid = hit.Parent.FindFirstChild("Humanoid")

	humanoid.Health = humanoid.Health - 60

end
script.Parent.Touched:Connect(onTouch())

this does not take away the humanoids health, could someone help me with this?

script.Disabled = true

You are disabling the script before taking away the health. Remove this line and it will work.

I made the script into this, but still. nothing is happened?

function onTouch(hit)

	local humanoid = hit.Parent.FindFirstChild("Humanoid")

	humanoid.Health = humanoid.Health - 60
	
	script.Disabled = true
	
end
script.Parent.Touched:Connect(onTouch())
script.Parent.Touched:Connect(onTouch())

You are calling a function here, you cannot do that as this parameter needs a function to be passed into it. So change this line to this:

script.Parent.Touched:Connect(onTouch)

You are also accessing the FindFirstChild method in a wrong way.

local humanoid = hit.Parent.FindFirstChild("Humanoid")

Change . to :.

local humanoid = hit.Parent:FindFirstChild("Humanoid")

Since you are calling the method on the hit.Parent.

1 Like

You aren’t letting the function complete before the script is disabled. A way to fix this is to use

Wait()

This will let the function be completed before the script is disabled. Code:

function onTouch(hit)

local humanoid = hit.Parent.FindFirstChild("Humanoid")

Humanoid.Health = health.Health - 60

wait() --in the middle of the brackets put what ever number you want the game to wait before the next function is executed.

script.Disabled = true
end
script.Parent.Touched:Connect(onTouch())

Do you want it to do damage only once, or do you want a debounce?

function onTouch(hit)

	local humanoid = hit.Parent:FindFirstChild("Humanoid")

	humanoid:TakeDamage(60)

end
script.Parent.Touched:Connect(onTouch)
1 Like

I Just want it to damage? sorry I just started last year I dont know all these stuff’s yet :woozy_face:

this has just failed the entire script, now it does not even play the swinging animation for the tool anymore

You could do

local val = false
script.Parent.Touched:Connect(function(hit)
	if not val then
		val = true
		local human = hit.Parent:FindFirstChild("Humanoid")
		if human then
			human.Health = human.Health - 60
			wait()
			script.Disabled = true
		end
		wait()
		val = false
	end
end)

Make sure the handle is touching the dummies when you test it.
The ‘val’ acts as a debounce.
Because you are disabling the script, it will only work once. (Unless you have another script going to enable it again).

Try this:

local function onPlayerTouch(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		
		Humanoid.Health -= 60
	end
end

script.Parent.Touched:Connect(onPlayerTouch)

You don’t want to disable the script right at the beginning so I would recommend doing something like this.

local connection
connection = script.parent.touched:connect(function(hit)

	local humanoid = hit.Parent.FindFirstChild("Humanoid")
if humanoid then
	humanoid.Health = humanoid.Health - 60
connection:Disconnect()
else 
Print(“I was not touched by a character!!”)

       end

end)

script.Parent.Touched:Connect(function(hit)
	-- Luau might not like hit.Parent directly but since hit is a BasePart and Touched only fires in Workspace, it should be expected to exist.
	local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if typeof(humanoid) == 'Instance' then
		humanoid:TakeDamage(60) -- Use the namecallmethod 'TakeDamage', it's built-in.
		script.Disabled = true
	end
end)
1 Like