Hello everyone I was working on a running system with animation for R6 characters when I was testing it it was working perfectly until the I started running and rotating then i found the animation was like stop and playing and the audio too
Here is the local Script
UIS.InputBegan:Connect(function(Key)--Running Function
if Key.KeyCode == Enum.KeyCode.LeftShift and canRun == true then
Running = true
canDo =false
Humanoid.WalkSpeed = 30
Humanoid.JumpPower = 0
canRun = false
Humanoid.Running:Connect(function(Speed)
if Speed > 0 and Humanoid.WalkSpeed ~=9 and Running == true and canRun == false then --When holding shift and player is running
Load3:Play()
script["Running Sound"]:Play()
tween3:Play()
elseif Speed == 0 then
script["Running Sound"]:Stop()
Load3:Stop()
tween4:Play()
end
UIS.InputEnded:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.LeftShift and canRun == false and crouched == false and sliding == false then
script["Running Sound"]:Stop()
tween4:Play()
canDo = true
canRun = true
Running = false
Humanoid.JumpPower = 50
Humanoid.WalkSpeed = 16
Load3:Stop()
end
end)
end)
end
end)
This code has several issues with logic and performance that need to be addressed. Here are a few recommendations for improvement:
Avoid Nesting InputEnded Inside InputBegan: Nesting InputEnded inside InputBegan can lead to multiple unnecessary connections being created. Itâs better to handle InputEnded separately.
Separate the Running:Connect Event: Connecting Running:Connect inside the InputBegan event will create multiple connections every time the LeftShift key is pressed. This is inefficient and can degrade performance over time.
Improve Code Readability and Organization: Refactor the code by creating separate functions for starting and stopping the running state. This approach will be better:
local UIS = game:GetService("UserInputService")
local Running = false
local canRun = true
local canDo = false
local crouched = false
local sliding = false
local function startRunning()
if canRun and not Running then
Running = true
canDo = false
canRun = false
Humanoid.WalkSpeed = 30
Humanoid.JumpPower = 0
script["Running Sound"]:Play()
tween3:Play()
end
end
local function stopRunning()
if Running then
Running = false
canDo = true
canRun = true
Humanoid.WalkSpeed = 16
Humanoid.JumpPower = 50
script["Running Sound"]:Stop()
tween4:Play()
end
end
UIS.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.LeftShift then
startRunning()
end
end)
UIS.InputEnded:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.LeftShift and not crouched and not sliding then
stopRunning()
end
end)
Humanoid.Running:Connect(function(Speed)
if Speed > 0 and Running then
Load3:Play()
elseif Speed == 0 then
Load3:Stop()
end
end)
This might not address your issue but itâs just painful reading that code.
I think the humanoid running logic is wrong, try this:
Humanoid.Running:Connect(function(Speed)
if Speed > 0 then
if Running then
Load3:Play()
else
Load3:Stop()
end
elseif Speed == 0 then
Load3:Stop()
end
end)
No worries i added my script back and made the âRunningâ bool turns true not when holding shift ,when holding shift and player is running anyways thank you all for your help