Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Audio spam plays when being damaged, wondering why (?)

Asked by 1 year ago

Hello!

I am trying to make a script Inside a NPC that plays a audio of the TF2 nope sound, as well of a dodge animation, the problem Is that when being damaged, It spams the Audio, and also wasting the times you need to hit It before being damaged

Script:

local Troll = script.Parent
local Dodge1 = Troll.Dodge1
local Dodge2 = Troll.Dodge2
local DodgeAbility = 100

while wait() do
    if Troll.Humanoid.Health < Troll.Humanoid.MaxHealth then
        DodgeAbility = DodgeAbility - 1
        if DodgeAbility < 2 then
            print("Angry")
            wait(10)
            DodgeAbility = 100
        else
            Troll.Nope:Play()
            local DodgeAnimationType = math.random(1,2)
            if DodgeAnimationType == 1 then
                local DodgeAnimation = Troll.Humanoid:FindFirstChild("Animator"):LoadAnimation(Dodge1)
                DodgeAnimation:Play()
            end
            if DodgeAnimationType == 2 then
                local DodgeAnimation = Troll.Humanoid:FindFirstChild("Animator"):LoadAnimation(Dodge2)
                DodgeAnimation:Play()
            end
        end
    end
end

1 answer

Log in to vote
1
Answered by 1 year ago

Please use PropertyChangedSignal, the while loop is unnecessary:

Also that's the reason why your audio spams:

Troll:WaitForChild("Humanoid"):GetPropertyChanged("Health"):Connect(function()
    -- CODE --
end)    
Ad

Answer this question