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

Play and Stop is not a valid member of Animation?

Asked by 4 years ago

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)
0
Test it again WideSteal321 773 — 4y
0
I believe you're looking for AnimationTrack:Play(). raid6n 2196 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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)
0
Line 6 will error, make sure you add an additional ". raid6n 2196 — 4y
Ad
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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()

Answer this question