My shift to sprint code not working

I was making a shift to sprint code and after i made it it’s not working…
Here is the code, I just got back to scripting after an year so i forgot some of it so i might have made some mistakes…

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input ,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = true
	end
end)
UIS.InputEnded:Connect(function(input,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = false
	end
end)

if keypressed then
	tw:Play()
	humanoid.WalkSpeed = 120
	
end
if not keypressed then
	rettw:Play()
	humanoid.WalkSpeed = 16
end

You’d need a loop or some kind to always detect if the keypressed is set to true or false. Otherwise it’ll just run once and never runs again afterwards.

try this script

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input ,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = true
	end
end)
UIS.InputEnded:Connect(function(input,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = false
	end
end)

repeat
task.wait()
if keypressed then
	tw:Play()
	humanoid.WalkSpeed = 120
	else
rettw:Play()
	humanoid.WalkSpeed = 16
end
until false

or even a better version would be

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input ,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		tw:Play()
	    humanoid.WalkSpeed = 120
	end
end)
UIS.InputEnded:Connect(function(input,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
	    rettw:Play()
	    humanoid.WalkSpeed = 16
	end
end)

But to add on this, usually what I do cause I don’t like loops for this. I’d make a boolvalue and parent it to somewhere connected to the player and then use the “Changed” event on the boolvalue so that you can detect it when its sets to true or false, and which is great for performance.

Yeah this is better, you don’t actually need anything. I thought you’d need smth like a loop.

Yes i did i already.
anyway it worked thnx for the help

1 Like

I just thought it would work with booleans so anyway it worked

Btw can anyone help how do I check if my player is running so i could execute the sprint code while running…

Just use walkspeed value as to detect if the player is “running” and I doubt you’ll need it to detect if the player is running. Just put it inside the userinput inputbegan

here’s a script for that

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input ,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = true
	end
end)
UIS.InputEnded:Connect(function(input,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = false
	end
end)

humanoid.StateChanged:Connect(function(v1)
    if v1 == Enum.HumanoidStateType.Running then
        tw:Play()
	    humanoid.WalkSpeed = 120
    else
        rettw:Play()
	    humanoid.WalkSpeed = 16
    end
end)

could u tell me what v1 is?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

and u didnt check on the keypressed boolean

v1 is the state of Humanoid (ex: running, jumping, died, etc…)

An sorry I forgot that, here’s a next version of script

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input ,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = true
	end
end)
UIS.InputEnded:Connect(function(input,gp)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = false
	end
end)

humanoid.StateChanged:Connect(function(v1)
    if v1 == Enum.HumanoidStateType.Running and keypressed then
        tw:Play()
	    humanoid.WalkSpeed = 120
    else
        rettw:Play()
	    humanoid.WalkSpeed = 16
    end
end)

Hi im back and your code doesen’t seem to be working…

--variables
local cam = workspace.CurrentCamera
local tweenservice = game:GetService("TweenService")
local sprintspeed = 120
local normalspeed = 16
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UIS = game:GetService("UserInputService")
local humanoid = char:WaitForChild("Humanoid")
local keypressed = false

--Fov
local info = TweenInfo.new(0.1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local fovgoal = {FieldOfView = 80}
local retinfo = TweenInfo.new(0.1,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut,0,false,0)
local retfovgoal = {FieldOfView = 70}
local tw = tweenservice:Create(cam,info,fovgoal)
local rettw = tweenservice:Create(cam,retinfo,retfovgoal)
--Input
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = true
	end
end)
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keypressed = false
	end
end)

humanoid.StateChanged:connect(function(state)
	if state == Enum.HumanoidStateType.Running and keypressed then
		tw:Play()
		humanoid.WalkSpeed = sprintspeed
	else
		rettw:Play()
		humanoid.WalkSpeed = normalspeed
	end
end)

Her’s the code i typed in

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.