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.
--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
--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.
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
--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)
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)