I’ve made a very basic FPS movement script (Crouching, Sprinting, Slow when aiming), and I’m
wonder if there are any potential issues that could lead inconsistencies in movement.
My Main concern is WalkSpeed won’t update properly (maybe it couldn’t track a change in the Attributes fast enough).
All the UserInputSerivce functions are controlled by a different local script, but both scripts are in StarterPlayerScripts
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local ts = game:GetService("TweenService")
local ss = game:GetService("SoundService")
local heavybreathing = ss:WaitForChild("Heavy Scared Breathing"):Clone()
local crouchanim = script:WaitForChild("Crouch")
local crouchtrack = hum:LoadAnimation(crouchanim)
local breathingvolume = 0.02
local walkspeedinfo = TweenInfo.new(0.25,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
local Crouchinginfo = TweenInfo.new(0.179,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
local breathinginfo = TweenInfo.new(9,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut)
local walkspeedtranstionup = ts:Create(hum,walkspeedinfo,{WalkSpeed = 22})
local walkspeedtranstiondown = ts:Create(hum,walkspeedinfo,{WalkSpeed = 16})
local Cwalkspeedtranstionup = ts:Create(hum,walkspeedinfo,{WalkSpeed = 16})
local Cwalkspeedtranstiondown = ts:Create(hum,walkspeedinfo,{WalkSpeed = 9})
local breathingup = ts:Create(heavybreathing,breathinginfo,{Volume = breathingvolume})
local breathingdown = ts:Create(heavybreathing,breathinginfo,{Volume = 0})
local Crouchingup = ts:Create(hum,Crouchinginfo,{CameraOffset = Vector3.new(0,0,-1.225)})
local Crouchingdown = ts:Create(hum,Crouchinginfo,{CameraOffset = Vector3.new(0,-1.5,-1.225)})
heavybreathing.Parent = char:WaitForChild("HumanoidRootPart")
heavybreathing.Volume = 0
heavybreathing.Looped = true
heavybreathing.Playing = true
function SETAIMSPEED(Movementtype)--So I can easily edit aim walkspeeds
if Movementtype == "Crouching" then
hum.WalkSpeed = 6
return
end
if Movementtype == "Walking" then
hum.WalkSpeed = 12
return
end
if Movementtype == "Normal" then
hum.WalkSpeed = 16
return
end
end
hum:GetAttributeChangedSignal("IsSprinting"):Connect(function()
if hum:GetAttribute("IsSprinting") then
if hum:GetAttribute("IsCrouching") then
Cwalkspeedtranstionup:Cancel()
Cwalkspeedtranstiondown:Cancel()
Crouchingdown:Cancel()
crouchtrack:Stop()
Crouchingup:Play()
end
if hum:GetAttribute("IsAimming") then
hum:SetAttribute("IsAimming", false)
end
walkspeedtranstionup:Play()
breathingup:Play()
elseif not hum:GetAttribute("IsSprinting") then
if hum:GetAttribute("IsAimming") or hum:GetAttribute("IsCrouching") then
walkspeedtranstionup:Cancel()
walkspeedtranstiondown:Cancel()
breathingup:Cancel()
breathingdown:Play()
return
end
walkspeedtranstiondown:Play()
breathingdown:Play()
end
end)
hum:GetAttributeChangedSignal("IsCrouching"):Connect(function()
if hum:GetAttribute("IsCrouching") == true then
if hum:GetAttribute("IsSprinting") then
walkspeedtranstionup:Cancel()
walkspeedtranstiondown:Cancel()
breathingup:Cancel()
breathingdown:Play()
end
if hum:GetAttribute("IsAimming") then
Cwalkspeedtranstiondown:Cancel()
Crouchingdown:Play()
crouchtrack:Play()--change to aim crouch
SETAIMSPEED("Crouching")
return
end
Cwalkspeedtranstiondown:Play()
Crouchingdown:Play()
crouchtrack:Play()
elseif hum:GetAttribute("IsCrouching") == false then
if hum:GetAttribute("IsSprinting") then
Cwalkspeedtranstionup:Cancel()
Cwalkspeedtranstiondown:Cancel()
Crouchingdown:Cancel()
Crouchingup:Cancel()
crouchtrack:Stop()
walkspeedtranstionup:Play()
breathingup:Play()
Crouchingup:Play()
return
end
if hum:GetAttribute("IsAimming") then
if hum:GetAttribute("IsSprinting") then
hum:SetAttribute("IsAimming", false)
Crouchingup:Play()
crouchtrack:Stop()
else
SETAIMSPEED("Walking")
Crouchingup:Play()
crouchtrack:Stop()
end
return
end
Cwalkspeedtranstionup:Play()
Crouchingup:Play()
crouchtrack:Stop()
end
end)
hum:GetAttributeChangedSignal("IsAimming"):Connect(function()
if hum:GetAttribute("IsAimming") then
if hum:GetAttribute("IsSprinting") then
walkspeedtranstionup:Cancel()
walkspeedtranstiondown:Cancel()
breathingup:Cancel()
breathingdown:Play()
SETAIMSPEED("Walking")
return
end
if hum:GetAttribute("IsCrouching") then
Cwalkspeedtranstiondown:Cancel()
Crouchingdown:Play()
crouchtrack:Play()--change to aim crouch
SETAIMSPEED("Crouching")
return
end
SETAIMSPEED("Walking")
else
if hum:GetAttribute("IsSprinting") then
walkspeedtranstionup:Cancel()
walkspeedtranstiondown:Cancel()
breathingdown:Cancel()
breathingup:Play()
walkspeedtranstionup:Play()
return
end
if hum:GetAttribute("IsCrouching") then
Cwalkspeedtranstiondown:Cancel()
Crouchingdown:Play()
crouchtrack:Play()
Cwalkspeedtranstiondown:Play()
return
end
SETAIMSPEED("Normal")
end
end)
hum:GetAttributeChangedSignal("IsJumping"):Connect(function()
if hum:GetAttribute("IsJumping") then
if hum:GetAttribute("IsCrouching") then
hum:SetAttribute("IsCrouching", false)
end
end
end)
--Debugging
game:GetService("RunService").Stepped:Connect(function()
print(hum.WalkSpeed)
end)