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