Hello everyone,
This script controls my sprint system I’ve been working on, which is in StarterCharacterScripts.
It works 100% of the time normally, except after reset where it doesn’t seem to work.
- meaning that the running/walking changes dont end up in effect at all.
Possible solutions:
- I read somewhere that this can be changed by adding a Player.CharacterAdded() within the script, but I tried this and it didnt work (at least for me / the way I did it.)
Any help is appreciated
Thanks
-- VARIABLES:
local Player = game.Players.LocalPlayer
local Humanoid = (Player.Character or Player.CharacterAdded:Wait()):FindFirstChild("Humanoid")
local PlayerCam = game.Workspace.Camera
local UIS = game:GetService("UserInputService")
local sprinting = false
-- ANIMATION SETTING:
local WalkAnim = Instance.new("Animation")
WalkAnim.Name = "WalkAnim"
WalkAnim.AnimationId = "rbxassetid://82821308814029"
local RunAnim = Instance.new("Animation")
RunAnim.Name = "RunAnim" RunAnim.AnimationId = "rbxassetid://82072058043542"
local WalkTrack = Humanoid:LoadAnimation(WalkAnim)
local RunTrack = Humanoid:LoadAnimation(RunAnim)
-- TWEENSERVICE CREATION:
local tweenservice = game:GetService("TweenService")
local TweenInfo = TweenInfo.new(0.167,Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false)
-- MAIN ANIMATION CONTROL (Fires when player presses control)
local function updateAnimation()
-- (If player is moving: )
if Humanoid.MoveDirection.Magnitude > 0 then
if sprinting then
if not RunTrack.IsPlaying then
WalkTrack:Stop()
RunTrack:Play()
end
else if not WalkTrack.IsPlaying then
RunTrack:Stop()
WalkTrack:Play()
end
end
else
--If the player ISNT moving, stop both tracks.
WalkTrack:Stop()
RunTrack:Stop()
end
end
-- INPUT DETECTION:
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
-- If the player isnt sprinting..
if sprinting == false then
sprinting = true
-- Fire the animation update function, which plays one track, and turns the other off.
updateAnimation()
-- Sets walkspeed and changes the FOV in the camera
Player.Character:FindFirstChild("Humanoid").WalkSpeed = 32
tweenservice:Create(PlayerCam,TweenInfo,{FieldOfView = 80}):Play()
else sprinting = false
-- Reverts
updateAnimation()
Player.Character:FindFirstChild("Humanoid").WalkSpeed = 16
tweenservice:Create(PlayerCam,TweenInfo,{FieldOfView = 70}):Play()
end
end
end)
-- This below is basically the saviour, if the player stops moving, it updates animation again, which will set both tracks to stop.
Humanoid.Running:Connect(updateAnimation)