Sprinting Not Working

Hi I am trying to make a sprinting script it works but when I reset my character in game it sometimes stops and doesn’t work and than when I reset again it sometimes works, any ideas on what to do?

local plr = game.Players.LocalPlayer

local function setupCharacter()
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")

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

	local RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Sprint"))
	local JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))

	-- Ensure the animation priority is set correctly
	RunAnim.Priority = Enum.AnimationPriority.Action
	JumpAnim.Priority = Enum.AnimationPriority.Action

	local Running = false
	local notMoving = true
	local isOnGround = true
	local Jumped = false
	local originalSpeed = Humanoid.WalkSpeed
	local db = true
	local lastPosition = Character.PrimaryPart.Position

	----------------- Changeable ---------------------
	local RunButton = Enum.KeyCode.LeftShift
	local newSpeed = 25
	local DefaultFieldOfView = 70
	local SprintFieldOfView = 85
	local MobileButton = plr.PlayerGui.MobileGUI.MobileButtons.RunButton
	--------------------------------------------------

	Camera.FieldOfView = DefaultFieldOfView

	local function Run()
		if not Running and isOnGround and script.RunEnabled.Value 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 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 not Running then
			Run()
		else
			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)

	RunService.Heartbeat:Connect(function()
		notMoving = Humanoid.MoveDirection.Magnitude == 0
		if notMoving then
			Stop()
		end

		if Running 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 and Humanoid:GetState() == Enum.HumanoidStateType.Jumping then
			Jumped = true
			RunAnim:Stop()
			JumpAnim:Play()
		end

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

		if Running and not script.RunEnabled.Value then
			Stop()
		end

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

		if isOnGround and Running and db 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 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)

	-- Disconnect previous connections to avoid multiple setups
	plr.CharacterRemoving:Connect(function()
		RunService:DisconnectAll()
		UIS:DisconnectAll()
	end)
end

-- Initial setup
setupCharacter()

-- Ensure setup on character respawn
plr.CharacterAdded:Connect(setupCharacter)

Screenshot 2024-07-29 211908

1 Like

Hi try changing this part of your script by this:
I tried and it worked fine with this for me. I think there are too reasons why it might not have worked , first when the player press Shift isTyping returns true .
Also why checking if the player is not running every frame with hearbeat it’s not necessary. That’s why i replaced with humanoid.Changed() instead

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

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

Humanoid.Changed:Connect(function()
	if Humanoid.MoveDirection.Magnitude == 0 then
		Stop()
	
		
	end
1 Like

So this is what I did after doing what you said and everything works in the testing area in roblox studio but when I reset the sprinting doesnt work (I made it so it checks if the player is not running every frame was to try and fix a animation thing but I forgot to get rid of it)

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

I’ve ran the entirety of your script except the mobile gui part and the only part i changed was removing

if isTyping then return end

Since when i press left shift , the input began function thinks i’m typing…
Look if isTyping is also set to true for you when you start pressing leftShift
When i run and reset everything works fine.

This is what I did for the sprinting script its still the same 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 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

And here is the mobile gui script since it might be the cause of the problem:

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

Here i try both the pc and mobile button to run , and both seems to be working fine even when i reset

Sorry, if I didn’t explain my problem properly, but the script doesnt work when you publish the game and than put it as public and play it in roblox not in roblox studio testing area.

It’s my fault here. Okay i’ve encoutered the problem when in-game and not in studio. Trying to figure out a solution right now

Okay so basically your script scripts starts with the first setup that sends the character of the player.
But if your character is loaded that doesn’t mean the parts inside of it are also.
Inside your script , you try to get the last position of the player using his primary part. But the player primairy part isnt loaded yet , so it cause an error.

To prevent this , the setup function must get the primary part when the primary part is indeed loaded.

I use a loop that will wait for the player primary part.
Also next time you have a problem send us the error in the output ( i would’ve understood the problem sooner )
Change the end of your script by this :

if plr.Character and plr.Character.PrimaryPart then
setupCharacter(plr.Character)
else

print("primary part not loaded")


while true and task.wait(0.1) do 
	if  plr.Character and plr.Character.PrimaryPart  then
		

		setupCharacter(plr.Character)
		
		break
		
		
	end
end

end

Is this what you mean? Since its not working but idk why.

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

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

local RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Sprint"))
local JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))
local Running = script:WaitForChild("IsRunning").Value
Running = false
local notMoving = true
local isOnGround = true
local Jumped = false
local originalSpeed = Humanoid.WalkSpeed
local db = true
local lastPosition = nil

----------------- Changeable ---------------------

local RunButton = Enum.KeyCode.LeftControl -- 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)
	-- Ensure the primary part is loaded
	while not character.PrimaryPart do
		character:GetPropertyChangedSignal("PrimaryPart"):Wait()
	end

	lastPosition = character.PrimaryPart.Position
end

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)

RunService.Heartbeat:Connect(function() 
	notMoving = Humanoid.MoveDirection.Magnitude == 0
	if notMoving then
		Stop()
	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)

if plr.Character and plr.Character.PrimaryPart then
	setupCharacter(plr.Character)
else
	print("Primary part not loaded")
	while true and task.wait(0.1) do 
		if plr.Character and plr.Character.PrimaryPart then
			setupCharacter(plr.Character)
			break
		end
	end
end

But now , when the player click or use another key he still runs but the camera field of view become become default again… idk how to fix it

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 originalSpeed = 16 -- Default Speed
local MobileButton = plr.PlayerGui.MobileGUI.MobileButtons.RunButton -- Location of the button


local Humanoid
local RunAnim
local JumpAnim

Camera.FieldOfView = DefaultFieldOfView



local function setupCharacter(character)
	local Humanoid = character:WaitForChild("Humanoid")
	 RunAnim = Humanoid:LoadAnimation(script:WaitForChild("Sprint"))
	JumpAnim = Humanoid:LoadAnimation(script:WaitForChild("Jump"))
	
	local Running = false
	local notMoving = true
	local isOnGround = true
	local Jumped = false
	
	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 input.KeyCode == RunButton then
			--print("running")
			Run()
		end
	end)

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

	Humanoid.Changed:Connect(function(property)
			notMoving = Humanoid.MoveDirection.Magnitude
			if notMoving == 0 then
				Stop()
			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

if plr.Character and plr.Character.PrimaryPart then
	setupCharacter(plr.Character)
	
	
else 
	
	print("primary part not loaded")
	while true and task.wait(0.1) do 
		if  plr.Character and plr.Character.PrimaryPart  then
			setupCharacter(plr.Character)
			
			break
			
			
		end
	end
	
	
end





Everything seems to be working now, only the mobile sprinting button doesn’t work.