Sprinting Not Working When I Reset

So I have a script that makes it so you sprint when you hold shift, it works in roblox studio testing area but sometimes when I reset in the actual published game I cant sprint when I respawn, any ideas on how to fix this problem?

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera

local RunButton = Enum.KeyCode.LeftShift -- Change this to the key you want for running
local newSpeed = 25 -- Speed you want while running
local DefaultFieldOfView = 70 -- Default FOV
local SprintFieldOfView = 85 -- Running FOV
local MobileButton = plr.PlayerGui.MobileGUI.MobileButtons.RunButton -- Location of the button

Camera.FieldOfView = DefaultFieldOfView

local function setupCharacter(character)
	local Humanoid = character:WaitForChild("Humanoid")
	local RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Sprint"))
	local JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))
	local Running = false
	local notMoving = true
	local isOnGround = true
	local Jumped = false
	local originalSpeed = Humanoid.WalkSpeed
	local db = true
	local lastPosition = character.PrimaryPart.Position

	local function Run()
		if Running == false and isOnGround == true and script.RunEnabled.Value == true and Humanoid.WalkSpeed == originalSpeed then
			Running = true
			RunAnim:Play()
			Humanoid.WalkSpeed = newSpeed

			local goal1 = { FieldOfView = SprintFieldOfView }
			local info1 = TweenInfo.new(0.5)
			local tween1 = TweenService:Create(Camera, info1, goal1)
			tween1:Play()
		end
	end

	local function Stop()
		if Running == true then
			Running = false
			RunAnim:Stop()
			Humanoid.WalkSpeed = originalSpeed

			local goal2 = { FieldOfView = DefaultFieldOfView }
			local info2 = TweenInfo.new(1)
			local tween2 = TweenService:Create(Camera, info2, goal2)
			tween2:Play()
		end
	end

	MobileButton.MouseButton1Click:Connect(function()
		if Running == false then
			Run()
		elseif Running == true then
			Stop()
		end
	end)

	UIS.InputBegan:Connect(function(input, isTyping)
		if isTyping then return end
		if input.KeyCode == RunButton then
			Run()
		end
	end)

	UIS.InputEnded:Connect(function(input)
		if input.KeyCode == RunButton then
			Stop()
		end
	end)

	Humanoid.Changed:Connect(function(property)
		if property == "MoveDirection" then
			notMoving = Humanoid.MoveDirection.Magnitude == 0
			if notMoving then
				Stop()
			end
		end

		if Running == true and (Humanoid:GetState() == Enum.HumanoidStateType.Physics or 
			Humanoid:GetState() == Enum.HumanoidStateType.Seated or 
			Humanoid:GetState() == Enum.HumanoidStateType.Swimming or 
			Humanoid:GetState() == Enum.HumanoidStateType.Climbing or 
			Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
			Stop()
		end

		if Running == true and Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
			Jumped = true
			RunAnim:Stop()
			JumpAnim:Play()
		end

		if Running == true and Jumped == true and Humanoid:GetState() == Enum.HumanoidStateType.Landed then
			Jumped = false
			RunAnim:Play()
		end

		if Running == true and script.RunEnabled.Value == false then
			Stop()
		end

		isOnGround = Humanoid.FloorMaterial ~= Enum.Material.Air

		if isOnGround and Running == true and db == true then
			db = false
			local dust = game.ReplicatedStorage.Fx.Dust:Clone()
			dust.Position = character:WaitForChild("HumanoidRootPart").Position + Vector3.new(0, -2.5, 0)
			dust.Parent = workspace.Fx
			dust.Name = "RunDust"
			dust.Attachment.Dust:Emit(1)
			game.Debris:AddItem(dust, 2.5)
			wait(0.15)
			db = true
		end

		if Running == true then
			for i = 1, math.random(1,2) do
				local rp = Instance.new("Part", workspace.Fx)
				rp.Anchored = true
				rp.CanCollide = false
				rp.Transparency = 0.8
				rp.Name = "RunParticle"
				rp.Material = Enum.Material.SmoothPlastic
				rp.CanQuery = false
				rp.Size = Vector3.new(0.05, 0.05, math.random(2.5, 3.5))
				local colorRandom = math.random(1,3)
				if colorRandom == 1 then
					rp.Color = Color3.fromRGB(107, 107, 107)
				elseif colorRandom == 2 then
					rp.Color = Color3.fromRGB(175, 175, 175)
				elseif colorRandom == 3 then
					rp.Color = Color3.fromRGB(148, 148, 148)
				end
				local dirCFrame = CFrame.new(lastPosition, character.PrimaryPart.Position)
				rp.CFrame = dirCFrame * CFrame.new(math.random(-25, 25)/10, math.random(-2.5, 2.5), math.random(-2, 2))
				game.Debris:AddItem(rp, 0.75)
				TweenService:Create(rp, TweenInfo.new(0.75), {Transparency = 1, Size = Vector3.new(0, 0, 0), CFrame = rp.CFrame * CFrame.new(0,0,math.random(2.5, 4))}):Play()
			end
		end
		lastPosition = character.PrimaryPart.Position
	end)
end

plr.CharacterAdded:Connect(setupCharacter)

-- Run setup for the initial character
if plr.Character then
	setupCharacter(plr.Character)
end

Screenshot 2024-07-29 211908

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunButton = Enum.KeyCode.LeftShift
local newSpeed = 25
local DefaultFieldOfView = 70
local SprintFieldOfView = 85

local function setupCharacter(character)
	local Humanoid = character:WaitForChild("Humanoid")
	local Camera = workspace.CurrentCamera
	Camera.FieldOfView = DefaultFieldOfView

	local Running = false
	local originalSpeed = Humanoid.WalkSpeed
	local lastPosition = character.PrimaryPart.Position

	local function Run()
		if not Running and Humanoid.FloorMaterial ~= Enum.Material.Air then
			Running = true
			Humanoid.WalkSpeed = newSpeed

			local goal1 = { FieldOfView = SprintFieldOfView }
			local info1 = TweenInfo.new(0.5)
			local tween1 = TweenService:Create(Camera, info1, goal1)
			tween1:Play()
		end
	end

	local function Stop()
		if Running then
			Running = false
			Humanoid.WalkSpeed = originalSpeed

			local goal2 = { FieldOfView = DefaultFieldOfView }
			local info2 = TweenInfo.new(1)
			local tween2 = TweenService:Create(Camera, info2, goal2)
			tween2:Play()
		end
	end

	local function onMobileButtonPressed()
		if not Running then
			Run()
		else
			Stop()
		end
	end

	local MobileButton = plr.PlayerGui:WaitForChild("MobileGUI"):WaitForChild("RunButton")
	MobileButton.MouseButton1Click:Connect(onMobileButtonPressed)

	UIS.InputBegan:Connect(function(input, isTyping)
		if input.KeyCode == RunButton then
			Run()
		end
	end)

	UIS.InputEnded:Connect(function(input)
		if input.KeyCode == RunButton then
			Stop()
		end
	end)

	Humanoid.Changed:Connect(function(property)
		if property == "MoveDirection" and Humanoid.MoveDirection.Magnitude == 0 then
			Stop()
		end
	end)
end

plr.CharacterAdded:Connect(setupCharacter)

if plr.Character then
	setupCharacter(plr.Character)
end

I removed the animation (:

you can add it if u want

1 Like

I tried the script but it doesn’t allow me to sprint anymore

I copied your script and modified it to work in a baseplate and it seemed to work fine after I reset. Do you have other scripts that modify the sprinting?

image
robloxapp-20240730-1131258.wmv (3.0 MB)
It works fine with me do u have any other scripts that modify the sprinting?

I have a mobile gui script:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Frame = script.Parent

function updateInput()
	local lastInput = UIS:GetLastInputType()
	if lastInput == Enum.UserInputType.Focus then return end
	Frame.Visible = lastInput == Enum.UserInputType.Touch
end

updateInput()
UIS.LastInputTypeChanged:Connect(updateInput)

RS.RenderStepped:Connect(function()
	if Frame.Visible then
		local screenSize = Frame.Screen.AbsoluteSize
		local minAxis = math.min(screenSize.X, screenSize.Y)
		local isSmallScreen = minAxis <= 500
		local jumpButtonSize = isSmallScreen and 70 or 120
		Frame.Size = UDim2.new(0, jumpButtonSize, 0, jumpButtonSize)
		Frame.Position = isSmallScreen and UDim2.new(1, -(jumpButtonSize*1.5-10), 1, -jumpButtonSize - 20) or UDim2.new(1, -(jumpButtonSize*1.5-10), 1, -jumpButtonSize * 1.75)
	end
end)

Screenshot 2024-07-30 202234

The sprint doesn’t always break when you reset like I had to reset around 5 times and than my sprint broke.