I just started studying how to script from posts and youtube videos and decided to make a simple SprintAndCrouch script inside StarterGui.
The Sprint and Crouch script used to be separate but one doesn't seem to work when the other does. The crouch speed changes properly but the sprint won't, and vice versa. I then decided to make it into one to disable sprinting when crouched which worked but I still can't find a way to make the sprinting part and crouch part of the script work properly together. Crouch_Speed, JumpPower, and the CameraOffset won't change but the animation plays. The Output tab doesn't show anything wrong and I've also tried comparing it to other similar scripts.
Can someone explain what I did wrong or what else should I do???
The LocalScript is as follows: Added spaces for clarity
local UserInputService = game:GetService("UserInputService")
local Crouch_Speed = 5
local SPRINT_SPEED = 25
local DEFAULT_SPEED = 10
local MAXIMUM_STAMINA = 100
local STAMINA_DECREASE = 0.25
local SPRINT_KEY = Enum.KeyCode.LeftShift
local StaminaAmount = MAXIMUM_STAMINA
local JumpHeight = 1
local crouching = false
local sprinting = false
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = script:WaitForChild("Animation")
local Track = Humanoid:LoadAnimation(Animation)
local Exterior = script.Parent.Exterior
local Bar = Exterior.Bar
UserInputService.InputBegan:Connect(function(Key, Processed)
if not Processed then if Key.KeyCode == SPRINT_KEY and crouching == false then sprinting = true end end
end)
UserInputService.InputEnded:Connect(function(Key, Processed)
if not Processed then if Key.KeyCode == SPRINT_KEY then sprinting = false end end
end)
game["Run Service"].RenderStepped:Connect(function()
if StaminaAmount > 0 then if sprinting then if Humanoid.MoveDirection.Magnitude > 0 then Humanoid.WalkSpeed = SPRINT_SPEED StaminaAmount -= STAMINA_DECREASE end else Humanoid.WalkSpeed = DEFAULT_SPEED end else Humanoid.WalkSpeed = DEFAULT_SPEED end if StaminaAmount < MAXIMUM_STAMINA then Exterior.Visible = true local Formula = (StaminaAmount/MAXIMUM_STAMINA) Bar:TweenSizeAndPosition(UDim2.new(1, 0, Formula, 0), UDim2.new(0, 0, 1-Formula, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1, true) if not sprinting then StaminaAmount += STAMINA_DECREASE end else Exterior.Visible = false end
end)
Track.Looped = true Track.Priority = Enum.AnimationPriority.Action
UserInputService.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.LeftControl and sprinting == false then crouching = true Humanoid.WalkSpeed = Crouch_Speed and Humanoid.JumpPower == JumpHeight Track:Play() end end
end)
UserInputService.InputEnded:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.LeftControl then crouching = false Track:Stop() end
end)