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

How do I use Animation.Looped = true properly?

I have an animation that I would like to loop when a value changes, but it doesn't loop for some reason; Here is my code:

local IsStunned = script.Parent:FindFirstChild("Stunned")
local Humanoid = script.Parent:WaitForChild("Humanoid")

local StunAnimation = workspace:WaitForChild("Stunned")
local PlayAnimation = Humanoid:LoadAnimation(StunAnimation)

local function StunPlayer()
	if IsStunned.Value == true then
		script.Parent.Humanoid.WalkSpeed = 0
		script.Parent.Humanoid.AutoRotate = false
		PlayAnimation:Play()
		PlayAnimation.Looped = true
	end
end

local function UnstunPlayer()
	if IsStunned.Value == false then
		PlayAnimation:Play()
		PlayAnimation.Looped = false
		script.Parent.Humanoid.WalkSpeed = 5
		wait(0.2)
		script.Parent.Humanoid.WalkSpeed = 16
		script.Parent.Humanoid.AutoRotate = true
		PlayAnimation.Looped = true
	end
end

IsStunned.Changed:Connect(StunPlayer)
IsStunned.Changed:Connect(UnstunPlayer)

2 answers

Hey there, this may not be a correct answer but try to change line 4 from workspace to game.Workspace. It may help, but I am not sure if it will work. Reply if it does not work, and I'll look more into it.

2 comments

0

Sorry, it didn't work.

0

Okay! Thank you for letting me know.

Well, I would recommend you to use only one function. Also, if you are putting AnimationTrack Looped = true, make sure to play animation after it.

This is how your script might look.

local IsStunned = script.Parent:FindFirstChild("Stunned")
local Humanoid = script.Parent:WaitForChild("Humanoid")

local StunAnimation = workspace:WaitForChild("Stunned")
local PlayAnimation = Humanoid:LoadAnimation(StunAnimation)

local function StunPlayerandUnStunPlayer()
    	if IsStunned.Value == true then
        	script.Parent.Humanoid.WalkSpeed = 0
        	script.Parent.Humanoid.AutoRotate = false
			PlayAnimation.Looped = true
        	PlayAnimation:Play()
	elseif IsStunned.Value == false then
        	PlayAnimation.Looped = false
			PlayAnimation:Play()
        	script.Parent.Humanoid.WalkSpeed = 5
        	wait(0.2)
        	script.Parent.Humanoid.WalkSpeed = 16
        	script.Parent.Humanoid.AutoRotate = true
			PlayAnimation.Looped = true
        	PlayAnimation:Play()
    	end
end

IsStunned.Changed:Connect(StunPlayerandUnStunPlayer)

Lemme know if it helps!

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