Hello. I am brand new to scripting in the past month and I am having troubles with a crouching animation script.
I have already created a function that will check if a player is moving, but it seems to build up lag over time. I want to figure out a way to eliminate the frame drops. I haven’t seen this anywhere else on the forum so I must be missing something silly.
I’ve tried using a coroutine and then yielding when uncrouching, but that didn’t work. I’ve also tried using RunService.Heartbeat/RenderStepped, but that made the problem worse.
Part of script that causes lag:
spawn(function()
while crouching == true do
wait()
print("Checking player movespeed")
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
track:AdjustSpeed(0.65)
else
track:AdjustSpeed(0)
end
end)
end
end)
Full script:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local sprintScript = character:WaitForChild("Sprint")
local crouchAnim = script.Crouch
local track = humanoid:LoadAnimation(crouchAnim)
track.Looped = true
track.Priority = Enum.AnimationPriority.Action
local crouching = false
local function checkKeyDown(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.C then
if not crouching then
crouching = true
sprintScript.Enabled = false
if humanoid.MoveDirection.Magnitude > 0 then
track:Play()
track:AdjustSpeed(0.65)
else
track:Play()
track:AdjustSpeed(0)
end
humanoid.WalkSpeed = 8
spawn(function()
while crouching == true do
wait()
print("Checking player movespeed")
humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if humanoid.MoveDirection.Magnitude > 0 then
track:AdjustSpeed(0.65)
else
track:AdjustSpeed(0)
end
end)
end
end)
elseif crouching then
crouching = false
sprintScript.Enabled = true
track:Stop()
humanoid.WalkSpeed = 16
end
end
end
end
UserInputService.InputBegan:Connect(checkKeyDown)