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())
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).
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)