Hello guys I was having issue with my sprinting script
The problem was when player press shift first and then walk, The script won’t make player run until they press more key while holding shift key and walking(This is because I update walking variable when player inputbegan so when player press shift and then walk, Humnaoid.MoveDirection.Magnitude is 0 and I also try update walking variable in while loop with runservice.Heartbeat:Wait() but that won’t work too)
Here is my code:
-- declare some service variables here
local Players = game:GetService("Players") -- get Players service and call it as Players
local TS = game:GetService("TweenService") -- get Tween service and call it as TS
local RS = game:GetService("ReplicatedStorage") -- get Replicated Storage service and call it as RS
local UIS = game:GetService("UserInputService") -- get User Input service and call it as UIS
local runservice = game:GetService("RunService") -- get Run service and call it as runservice
-- declare some service variables here
-- declare some player and character variable here
local plr = Players.LocalPlayer -- get local player and call it as plr
local chr = script.Parent -- get local player character and call it as chr
-- declare some player and character variable here
-- declare some module scripts here
local plr_data = require(RS.plr_data) -- get plr_data module script which is stored all player datas and call it as plr_data
-- declare some module scripts here
-- declare some player's character parts here
local HUM = chr:FindFirstChildWhichIsA("Humanoid") -- get player character's humanoid and call it as HUM
-- delcare some player's character parts here
-- declare some number variables here
local walk_speed = 16 -- walk speed
local run_speed = 32 -- run speed
local walk_fov = 70 -- walk fov
local run_fov = 80 -- run fov
local tween_walk_time = 0.25 -- tween fov walk time
local tween_run_time = 0.25 -- tween fov run time
-- declare some number variables here
-- declare some bool variables here
local walking = false
local sprinting = false
-- declare some bool variables here
-- declare some game objects here
local camera = workspace.CurrentCamera -- camera to tween field of view
-- declare some game objects here
-- declare some tween settings here
local tween_walk_info = TweenInfo.new(tween_walk_time, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0) -- tween walk info
local tween_run_info = TweenInfo.new(tween_run_time, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0) -- tween run info
local tween_walk = TS:Create(camera, tween_walk_info, {FieldOfView = walk_fov}) -- tween walk
local tween_run = TS:Create(camera, tween_run_info, {FieldOfView = run_fov}) -- tween run
-- declare some tween settings here
-- do something here
while not plr_data["plr_loaded"] do runservice.Heartbeat:Wait() end -- wait until player loaded
if UIS.KeyboardEnabled and UIS.MouseEnabled and not UIS.TouchEnabled and not UIS.VREnabled then -- if player is playing on pc
UIS.InputBegan:Connect(function(input, chatting) -- detect when input begin
if chatting then return end -- if player is chatting then return
if (HUM.MoveDirection.Magnitude) > 0 then -- if player is walking
walking = true -- set walking variable to true
end
if input.KeyCode == Enum.KeyCode.LeftShift then -- if player pressing left shift
sprinting = true -- set sprinting to true
end
if walking and sprinting then -- if walking and sprinting are true
HUM.WalkSpeed = run_speed -- set player walk speed to run_speed(32)
tween_run:Play() -- play tween_run to tween camera Fov
end
end)
UIS.InputEnded:Connect(function(input, chatting) -- detect when input ended
if chatting then return end -- if player is chatting then return
if (HUM.MoveDirection.Magnitude) <= 0 then -- if player is not walking
walking = false -- set walking variable to false
end
if input.KeyCode == Enum.KeyCode.LeftShift then -- if player stop pressing left shift
sprinting = false -- set sprinting to false
end
if not walking or not sprinting then -- if walking or sprinting are false
HUM.WalkSpeed = walk_speed -- set player walk speed to run_speed(16)
tween_walk:Play() -- play tween_walk to tween camera Fov
end
end)
else
-- future :)
end
-- do something here
So I wanna make my sprint script work when player press shift first then walk or walk first then press shift so if anyone know how to make Humanoid.MoveDirection.Magnitude work fine for my script or got any advice for my script you can comment I’ll listen to all opinions and sorry if I say something wrong.
Thank you