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

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

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

✓ Accepted answer

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)	

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