Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local UserInputService = game:GetService("UserInputService")
- local character = script.Parent
- local humanoid = character.Humanoid
- local WALKSPEED = 15
- local RUNSPEED = 20
- local walkAnim = humanoid:LoadAnimation(WALKANIMATION)
- local runAnim = humanoid:LoadAnimation(RUNANIMATION)
- local idleAnim = humanoid:LoadAnimation(IDLEANIMATION)
- local runKeyCode = Enum.KeyCode.LeftShift
- local currentlyMoving = false -- set to true whenever move direction not 0
- local currentlyIdle = false -- idle state, true whenever running (idle anim playing)
- local currentlyRunning = false -- run state, true whenever running (running anim playing)
- local currentlyWalking = false -- walk state, true whenever running (walk anim playing)
- local holdingShift = true
- local function run()
- if currentlyIdle then
- currentlyIdle = false
- idleAnim:Stop()
- end
- if currentlyWalking then
- currentlyWalking = false
- walkAnim:Stop()
- end
- currentlyRunning = true
- humanoid.WalkSpeed = RUNSPEED
- runAnim:Play()
- end
- local function walk()
- if currentlyIdle then
- currentlyIdle = false
- idleAnim:Stop()
- end
- if currentlyRunning then
- currentlyRunning = false
- runAnim:Stop()
- end
- currentlyWalking = true
- humanoid.WalkSpeed = WALKSPEED
- walkAnim:Play()
- end
- local function idle()
- if currentlyWalking then
- currentlyWalking = false
- walkAnim:Stop()
- end
- if currentlyRunning then
- currentlyRunning = false
- runAnim:Stop()
- end
- currentlyIdle = true
- idleAnim:Play()
- end
- UserInputService.InputBegan:Connect(function(input, gameprocess)
- if gameprocess or not input.KeyCode == runKeyCode then return end
- holdingShift = true
- if currentlyMoving then
- run()
- end
- end)
- UserInputService.InputEnded:Connect(function(input, gameprocess)
- if gameprocess or not input.KeyCode == runKeyCode then return end
- holdingShift = false
- if currentlyMoving and currentlyRunning then
- walk()
- end
- end)
- humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
- if humanoid.MoveDirection.Magnitude > 0 then
- if currentlyMoving then return end
- currentlyMoving = true
- if holdingShift then
- run()
- else
- walk()
- end
- else
- currentlyMoving = false
- idle()
- end
- end)
- idle() -- default state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement