So the title is quite self explanatory, i need it to consistently check the value so it could also change the speed. I have tried .Changed but it just breaks my code, and i really dont know where to place it so i really need your help.
The Code:
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
--These are the values to be consistently checked
local maxSpeed = Character.RunningScript.Speed.Value --self explanatory
local acceleration = Character.RunningScript.Acceleration.Value --its kinda self explanatory also
local currentSpeed = 18
local currentAnimSpeed = 0.4
local isAccelerating = false
local isRunning = false
local runSound = game.SoundService.Running
runSound.PlaybackSpeed = currentSpeed / maxSpeed
local function increaseSpeed()
if currentSpeed < maxSpeed then
currentSpeed = currentSpeed + acceleration
currentAnimSpeed = currentAnimSpeed + (acceleration / maxSpeed)
else
currentSpeed = maxSpeed
currentAnimSpeed = 1.1
isAccelerating = false
end
Character.Humanoid.WalkSpeed = currentSpeed
PlayAnim:AdjustSpeed(currentAnimSpeed)
runSound.PlaybackSpeed = currentSpeed / maxSpeed
end
function ToggleAnimationAndSound(shouldPlay)
if shouldPlay then
if not PlayAnim.IsPlaying then
PlayAnim:Play()
end
if not runSound.IsPlaying then
runSound:Play()
end
else
PlayAnim:Stop()
runSound:Stop()
end
end
local leftControlDown = false
local heartbeatflag = false
game:GetService('RunService').Heartbeat:Connect(function()
ToggleRunningIfAirborne()
if isAccelerating then
increaseSpeed()
end
if Character.Humanoid.MoveDirection ~= Vector3.new() then
if heartbeatflag then return end
heartbeatflag = true
isRunning = true
isAccelerating = true
local Anim = script.SprintAnim
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
ToggleAnimationAndSound(true)
print('Running')
else
if not heartbeatflag then return end
heartbeatflag = false
Character.Humanoid.WalkSpeed = 13
ToggleAnimationAndSound(false)
currentSpeed = 13
currentAnimSpeed = 0.4
isAccelerating = false
isRunning = false
print('Running stopped')
end
end)
function ToggleRunningIfAirborne()
if Character.Humanoid.FloorMaterial == Enum.Material.Air or leftControlDown then
ToggleAnimationAndSound(false)
elseif isRunning then
ToggleAnimationAndSound(true)
end
end
game:GetService('RunService').Heartbeat:Connect(function()
ToggleRunningIfAirborne()
if isAccelerating then
increaseSpeed()
end
end)