Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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:

-- Services
local runService = game:GetService("RunService")

local tweenService = game:GetService("TweenService")
local playersService = game:GetService("Players")

-- Variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera

-- Settings
local normalSpeed = 16
local sprintSpeed = 26

local normalFov = 70
local sprintFov = 80

-- Boolean
local running = script:WaitForChild("Running")

-- Animation
local animation = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(animation)

-- Connections
local inputBeganConnection
local inputEndedConnection

-- // Functions

-- Camera tweening handler
local tween_fov = function(duration, value)
	local fov_tween = tweenService:Create(camera, TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {FieldOfView = value})
	fov_tween:Play()
	return fov_tween
end

-- Input controller
function onKeyPress(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		if humanoid.MoveDirection.Magnitude <= 0 then
					running.Value = true

					humanoid.WalkSpeed = sprintSpeed
				else
					running.Value = true

					humanoid.WalkSpeed = sprintSpeed
					sprintTrack:Play(0.25)
				end
			end
		

	
	if userInputState == Enum.UserInputState.End then	
				running.Value = false
				humanoid.WalkSpeed = normalSpeed
				sprintTrack:Stop(0.25)
		onKeyPress(actionName, userInputState, inputObject):Disconnect()
end
end

		
	

-- Field of view controller
running.Changed:Connect(function()
	if running.Value then
		tween_fov(0.5, sprintFov)
	elseif not running.Value then
		tween_fov(0.5, normalFov)
	end
end)

-- Main loop controller
runService.RenderStepped:Connect(function()
	if running.Value then
		-- Check if player is moving
		if humanoid.MoveDirection.Magnitude <= 0 then
			if sprintTrack.IsPlaying then
				sprintTrack:Stop(0.25)
			end
		elseif humanoid.MoveDirection.Magnitude > 0 then
			if not sprintTrack.IsPlaying then
				sprintTrack:Play(0.25)
			end
		end

		-- Check if player is jumping
		if humanoid.FloorMaterial == Enum.Material.Air then
			if sprintTrack.IsPlaying then
				sprintTrack:Stop(0.1)
			end
		elseif humanoid.FloorMaterial ~= Enum.Material.Air and humanoid.MoveDirection.Magnitude > 0 then
			if not sprintTrack.IsPlaying then
				sprintTrack:Play(0.25)
			end
		end
	end
end)

game.ContextActionService:BindAction("keyPressSpecialName", onKeyPress, true, Enum.KeyCode.C)
game.ContextActionService:SetPosition("keyPressSpecialName", UDim2.new(.5,0,-.5,0))
game.ContextActionService:SetImage("keyPressSpecialName", "rbxassetid://73737626")

2 answers

✓ Accepted answer

We talked about this in chat nobs.

The error is doing it because it's constantly being interfered with. On line 49, your character will constantly be running I think? Correct me if I'm wrong. Not sure how to fix it.

2 comments

0

After not holding down c the player stops sprinting, the game just freezes

0

No clue...

This discussion is preserved as it was. New answers, comments, and votes are closed.