Hi, I am trying to make a realistic Walk/Sprint system but for some reason the animation wont change when I sprint.
here’s my code :
game.Players.LocalPlayer.Character:WaitForChild("Animate").run.RunAnim.AnimationId = "http://www.roblox.com/asset/?id=2510202577"
local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
mouse.KeyDown:connect(function (key) -- Run function
key = string.lower(key)
if string.byte(key) == 48 then
running = true
local keyConnection = mouse.KeyUp:connect(function (key)
if string.byte(key) == 48 then
running = false
end
end)
game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId ="http://www.roblox.com/asset/?id=913402848"
for i = 1,30 do
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed + 0.3
game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView + 0.5
wait()
end
repeat wait () until running == false
keyConnection:disconnect()
game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId ="http://www.roblox.com/asset/?id=2510202577"
for i = 1,30 do
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed - 0.3
game.Workspace.CurrentCamera.FieldOfView = game.Workspace.CurrentCamera.FieldOfView - 0.5
wait()
end
end
end)
Hi!
I changed your code a little bit so that it works faster. Changing default animation each time player presses LeftShift drains performance. Instead, this script only plays an already created animation instance. When sprinting stops, this animation stops and default run animation plays. Don’t forget that on PC, run animation is the same for walking and running, because walking animation is somehow only triggered at lower walking speeds, which happen when games are played on game consoles, tablets or phones.
One more thing[!]. If you want your animations to work outside studio and for other players in publicly accessible game, they must be owned by you or the group that published the game!
Some lines are just shortened for the sake of readability. The following code should work (unfortunately, I didn’t get the chance to test it). I’ll test it myself when I find time and will update in case I discover any errors.
local Players = game:GetService('Players')
local character = Players.LocalPlayer.Character or Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild('Humanoid')
local camera = game.Workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
-- Set default animation (remember that run animation is used for both walking and running on PC)
character:WaitForChild("Animate").run.RunAnim.AnimationId = "rbxassetid://2510202577"
-- Add new animation to character
local runTrack = Instance.new('Animation') -- new instance and its location
runTrack.Name = 'CustomRunAnimation'
runTrack.AnimationId = "rbxassetid://913402848"
local runAnim = humanoid:LoadAnimation(runTrack)
-- Run function
mouse.KeyDown:connect(function (key)
key = string.lower(key)
if string.byte(key) == 48 then
running = true
local keyConnection = mouse.KeyUp:connect(function (key)
if string.byte(key) == 48 then
running = false
end
end)
for i = 1,30 do
humanoid.WalkSpeed = humanoid.WalkSpeed + 0.3
camera.FieldOfView = camera.FieldOfView + 0.5
wait()
end
if runAnim.IsPlaying ~= true then
runAnim:Play() -- overwrite current animation
end
repeat wait () until running == false
keyConnection:disconnect()
runAnim:Stop() -- stop playing animation
for i = 1,30 do
humanoid.WalkSpeed = humanoid.WalkSpeed - 0.3
camera.FieldOfView = camera.FieldOfView - 0.5
wait()
end
end
end)
I also suggest you look into TweenService. In fact, it could very much improve FOV and WalkSpeed change (smooth transitions). You can read about it here: