So, I tried disconnecting my OnKeyPress Function, and stopping the player from sprinting?
So, I tried disconnecting my OnKeyPress Function, and stopping the player from sprinting. A bigger problem them comes into the script.
My roblox studio freezes for like 10 seconds after undoing the sprint.
I know why, I just don't know how to fix it
error:
https://imgur.com/a/6ow6MU7
Script:
002 | local runService = game:GetService( "RunService" ) |
004 | local tweenService = game:GetService( "TweenService" ) |
005 | local playersService = game:GetService( "Players" ) |
008 | local player = playersService.LocalPlayer |
009 | local character = player.Character or player.CharacterAdded:Wait() |
010 | local humanoid = character:WaitForChild( "Humanoid" ) |
012 | local camera = workspace.CurrentCamera |
015 | local normalSpeed = 16 |
016 | local sprintSpeed = 26 |
022 | local running = script:WaitForChild( "Running" ) |
025 | local animation = script:WaitForChild( "SprintAnim" ) |
026 | local sprintTrack = humanoid:LoadAnimation(animation) |
029 | local inputBeganConnection |
030 | local inputEndedConnection |
035 | local tween_fov = function (duration, value) |
036 | local fov_tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), { FieldOfView = value } ) |
042 | function onKeyPress(actionName, userInputState, inputObject) |
043 | if userInputState = = Enum.UserInputState.Begin then |
044 | if humanoid.MoveDirection.Magnitude < = 0 then |
047 | humanoid.WalkSpeed = sprintSpeed |
051 | humanoid.WalkSpeed = sprintSpeed |
052 | sprintTrack:Play( 0.25 ) |
058 | if userInputState = = Enum.UserInputState.End then |
059 | running.Value = false |
060 | humanoid.WalkSpeed = normalSpeed |
061 | sprintTrack:Stop( 0.25 ) |
062 | onKeyPress(actionName, userInputState, inputObject):Disconnect() |
070 | running.Changed:Connect( function () |
071 | if running.Value then |
072 | tween_fov( 0.5 , sprintFov) |
073 | elseif not running.Value then |
074 | tween_fov( 0.5 , normalFov) |
079 | runService.RenderStepped:Connect( function () |
080 | if running.Value then |
082 | if humanoid.MoveDirection.Magnitude < = 0 then |
083 | if sprintTrack.IsPlaying then |
084 | sprintTrack:Stop( 0.25 ) |
086 | elseif humanoid.MoveDirection.Magnitude > 0 then |
087 | if not sprintTrack.IsPlaying then |
088 | sprintTrack:Play( 0.25 ) |
093 | if humanoid.FloorMaterial = = Enum.Material.Air then |
094 | if sprintTrack.IsPlaying then |
095 | sprintTrack:Stop( 0.1 ) |
097 | elseif humanoid.FloorMaterial ~ = Enum.Material.Air and humanoid.MoveDirection.Magnitude > 0 then |
098 | if not sprintTrack.IsPlaying then |
099 | sprintTrack:Play( 0.25 ) |
105 | game.ContextActionService:BindAction( "keyPressSpecialName" , onKeyPress, true , Enum.KeyCode.C) |
106 | game.ContextActionService:SetPosition( "keyPressSpecialName" , UDim 2. new(. 5 , 0 ,-. 5 , 0 )) |