Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My DamageScript for a mob wont wait before hitting the player. Why?

function onTouched(hit)
	wait(1) -- I added the wait in hopes to have the zombie not infinitely hit the player.
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 1
        end
end
script.Parent.Touched:connect(onTouched)

This script is supposed to deduct damage from the player and wait a second before hitting them again. I added the wait in there in hopes to make the enemy not infinitely hit the player, but it still does. Can someone tell me what I'm doing wrong? Thanks.

1 answer

Try using debounce, and you can also do :TakeDamage() for the humanoid.

Edited Script

local debounce = true
local coolDown = 2 --// Change this to the amount the zombie has to wait to hit again
local damage = 10 --// Change this to the amount of damage you want the player to take

function onTouched(hit)
	local human = hit.Parent:findFirstChild("Humanoid")
	if human ~= nil and debounce == true then
		debounce = false
		human:TakeDamage(damage)
		wait(coolDown)
		debounce = true
	end
end

script.Parent.Touched:connect(onTouched)

This will check if debounce is true or not, if it is, it will set it to false, damage the player, wait the cooldown time, and then set it to true again and then it will be able to attack it again. There are 2 things that are changeable and have an effect on the zombie's attacking behavior.

Hope this helped! If this worked, let me know by selecting this as the answer! Just make sure you put this script inside of a part inside the zombie. I recommend putting this script inside of the HumanoidRootPart OR Torso.

This discussion is preserved as it was. New answers, comments, and votes are closed.