Animation wont play on value.Changed

Hello, im trying to make a stun so the opponent cant attack while being stunned by the enemy, i made an animation for the hit and for some reason it doesnt play. Here is the code.

local stun = script.Parent.stun
--7899158637
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7899169009"
local humanoid = script.Parent.Humanoid
local animationtrack = humanoid:LoadAnimation(animation)
stun.Changed:Connect(function()
	if stun == true then
		animationtrack:Play()
		script.Parent.Humanoid.WalkSpeed = 0
		wait(1)
	elseif stun == false then
		script.Parent.Humanoid.WalkSpeed = 16
	end
end)

I’ve tried stun.Value.Changed too, but that didnt seem to work. Also, the script is inside a dummy since i dont have anyone to try on.

Did you ever change the value of stun? If not, then that could be a reason why it’s not working.

Another thing you could try is using “GetPropertyChangedSignal” on the value property of stun. It basically works the same way as .Changed, but it’s a little more accurate.

If these don’t work, sending your code where you change the stun value would be helpful. Thanks.

local stun = script.Parent.stun
--7899158637
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7899169009"
local humanoid = script.Parent.Humanoid
local animationtrack = humanoid:LoadAnimation(animation)
stun:GetPropertyChangedSignal("Value"):Connect(function()
	if stun == true then
		animationtrack:Play()
		script.Parent.Humanoid.WalkSpeed = 0
		wait(1)
	elseif stun == false then
		script.Parent.Humanoid.WalkSpeed = 16
	end
end)

Yes! in my combat tool it changes it on hit.

I’ll see if this works. Thank you!

my pleasure always here to help players in need

You literally just restated what I already told him, but whatever.

oh i am sorry i didnt saw your answer hehe

This did not work. Here is the combat tool.


--7891359828
--7891483908

local Player = script.Parent.Parent.Parent
Player.CharacterAdded:Wait()

local Character = script.Parent.Parent.Parent.Character
local dmgpoint1 = Instance.new("Attachment")
dmgpoint1.Position = Vector3.new(0, -2.198, 0)
dmgpoint1.Name = "DmgPoint"
dmgpoint1.Parent = Character["Right Arm"]

local dmgpoint2 = Instance.new("Attachment")
dmgpoint2.Position = Vector3.new(0, -2.198, 0)
dmgpoint2.Name = "DmgPoint"
dmgpoint2.Parent = Character["Left Arm"]

local dmgpoint3 = Instance.new("Attachment")
dmgpoint3.Position = Vector3.new(0, -2.052, -0.662)
dmgpoint3.Name = "DmgPoint"
dmgpoint3.Parent = Character["Left Leg"]

local dmgpoint4 = Instance.new("Attachment")
dmgpoint4.Position = Vector3.new(0, -2.052, -0.662)
dmgpoint4.Name = "DmgPoint"
dmgpoint4.Parent = Character["Right Leg"]


local raycastHitbox = require(game:GetService("ReplicatedStorage").RaycastHitboxV4)
local Hitbox = raycastHitbox.new(Character)
local stun = Instance.new("BoolValue")
stun.Value = false
stun.Name = "stun"
stun.Parent = Player
print(Character.Name)
local combo = 0
local Damage = 10
local player = script.Parent.Parent.Parent.Character
local humanoid = player:WaitForChild("Humanoid")
wait(1)
local idleanim = Instance.new("Animation")
idleanim.AnimationId = "rbxassetid://7891324866"
local idletrack = humanoid:LoadAnimation(idleanim)
local attackanim = Instance.new("Animation")
attackanim.AnimationId = "rbxassetid://7891446786"
local attacktrack = humanoid:LoadAnimation(attackanim)
local attackanim2 = Instance.new("Animation")
attackanim2.AnimationId = "rbxassetid://7891598405"
local attacktrack2 = humanoid:LoadAnimation(attackanim2)
local attackanim3 = Instance.new("Animation")
attackanim3.AnimationId = "rbxassetid://7891627933"

local db = false
local attacktrack3 = humanoid:LoadAnimation(attackanim3)
script.Parent.Activated:Connect(function()
	if stun.Value == false then
		if db == false then
			Character.Humanoid.WalkSpeed = 16
			db = true
			Hitbox:HitStart()
			Hitbox.OnHit:Connect(function(hit, humanoid)
				if humanoid.Parent.Name ~= script.Parent.Parent.Name then
					humanoid:TakeDamage(Damage)
					script.Parent.Sound:Play()
					humanoid.Parent.stun.Value = true
					wait(0.5)
					humanoid.Parent.stun.Value = false
				end
			end)
			if combo == 0 then
				attacktrack:Play()
				combo += 1
			elseif combo == 1 then
				attacktrack2:Play()
				combo += 1
			elseif combo == 2 then
				attacktrack3:Play()
				combo = 0
			end
			wait(0.2)
			Hitbox:HitStop()
			wait(0.1)
			Character.Humanoid.WalkSpeed = 16
			db = false
		end
	end
end)

script.Parent.Equipped:Connect(function()
	idletrack:Play()
end)

script.Parent.Unequipped:Connect(function()
	idletrack:Stop()
end)

does it has a error use prints and tell me where it stops

It doesnt have an error and i can check that the script on hit has run due to the humanoid health going down

Where are your two scripts located?

I also think that waiting 0.5 seconds is kind of a short time for an animation to play.

well when the stun is active itll make them imobile so false = not stunned true = stunned
also it isnt a big animation
cccomnbat script is in the tool and the npc script is inside the npc and the player

Alright. Make sure the dummy is not anchored because I don’t think animations play on anchored characters. If you need to, just set up a local server with 2 players in it, and test it that way.

Also, your pathways for your scripts don’t really make sense considering that NPCs don’t have a player object. I would instead parent the “stun” value to characters and not players.

the stun does get parented to the character
just if you were wondering

local stun = script.Parent:WaitForChild("stun")
--7899158637
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7899169009"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationtrack = animator:LoadAnimation(animation)
stun.Changed:Connect(function()
	if stun then
		animationtrack:Play()
		humanoid.WalkSpeed = 0
		task.wait(5)
		stun.Value = false
		humanoid.WalkSpeed = 16
	elseif not stun then
		humanoid.WalkSpeed = 16
	end
end)

Use the 2 player test mode.

It doesnt work, it must be the stun.Value = true/false in the combat script.
Edit: I observed the stun value on the npc and it seems to actually change, im really confused.

Found the issue, the waitforchild was infinitely waiting and stopping the script from executing.

That means that “humanoid” isn’t correctly being referenced.

found the issue which was causing it im not 100% sure but now it works, thanks.