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

Play and Stop is not a valid member of Animation?

Ok so i want to make a Sprint animation, And of course this is the code, When i put the Animation inside there, It says "Play and Stop is not a valid Member of Animation."

local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local animation = Instance.new("Animation")
local normalSpeed = 15 -- The player's speed while not sprinting
local sprintSpeed = 25 -- The player's speed while sprinting
local sprinting = false

inputService.InputBegan:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.LeftControl then
		running = true
		if char.Humanoid then	
			char.Humanoid.WalkSpeed = sprintSpeed
			animation.AnimationId = "http://www.roblox.com/asset/?id=5627066906"
			animation:Play()
		end
	end
end)

inputService.InputEnded:Connect(function (key)
	if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.LeftControl then
		running = false
		if char.Humanoid then	
			char.Humanoid.WalkSpeed = normalSpeed
			animation:Stop() 
		end
	end
end)

2 comments

0

Test it again

0

I believe you're looking for AnimationTrack:Play().

2 answers

You have to load the animation before playing it.

local inputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=5627066906
local run = humanoid:LoadAnimation(animation)
local normalSpeed = 15 -- The player's speed while not sprinting
local sprintSpeed = 25 -- The player's speed while sprinting
local sprinting = false

inputService.InputBegan:Connect(function (key)
    if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.LeftControl then
        running = true
        if char.Humanoid then   
            char.Humanoid.WalkSpeed = sprintSpeed
            run:Play()
        end
    end
end)

inputService.InputEnded:Connect(function (key)
    if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.LeftControl then
        running = false
        if char.Humanoid then   
            char.Humanoid.WalkSpeed = normalSpeed
            run:Stop()
        end
    end
end)

1 comment

0

Line 6 will error, make sure you add an additional ".

I believe you're looking for AnimationTrack:Play(). https://developer.roblox.com/en-us/api-reference/class/AnimationTrack

Example:

local character = player.Character 
local humanoid = character:FindFirstChild("Humanoid")
 
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=507771019" -- Roblox dance emote
 
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()

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