Hi, I have an NPC enemy who, upon spotting a player, plays a sound and chases them if the player is in their line of sight. I added a function to play an individual sound and added a debounce/cooldown to this function so the sound would not loop over and over if the player was in his line of sight (it’s loud)
This is the code I wrote:
local function roar()
local roar = script.Parent.Head.Roar
if debounce then return end
debounce = true
roar:Play()
roar.Ended:Wait(5)
debounce = false
end
And as it turns out, this breaks his behaviour. He waits 5 seconds and chases again until I get out of his line of sight. I don’t want the whole code to break and I know the debounce has something to do with it, I just don’t know what I can do to stop this behaviour.
If it helps,
This is the function the other function is used in
local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 100
local nearestTarget
local shockSound = script.Parent.Head.Shock
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local distance = (xan.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < maxDistance and canSeeTarget(target) then
humanoid.WalkSpeed = 21
nearestTarget = target
maxDistance = distance
shockSound:Play()
roar()
else
humanoid.WalkSpeed = 16
end
end
end
return nearestTarget
end
I should also mention, the whole thing works fine. The only thing that isn’t supposed to happen is the enemy stopping for 5 seconds then continuing
Any help is appreciated!