Looping Assistance

Hello,

So I have a script that working perfectly fine but theres a problem. I want to add a loop so that it can switch between animations when moving and stopped. But… I sadly don’t know how to or where to add the loop. Help please.

I don’t know if I understood you correctly but take a look at this:

local function simpleFunction()
	task.spawn(function()
		while task.wait(1) do
			print("I keep running while the code below is running!")
		end
	end)
	
	--The rest of the code here (this piece of code will run even if the loop above is running)...
	print("I'm a command!")
end
-- QB Mode --
function QBOn()
	script.Mode.Enabled = true
	Character:FindFirstChild('FB'):SetAttribute('QBMode',true)
	task.spawn(function()
		while task.wait(1) do
			print('Ede')
		end
	end)
	if Humanoid.MoveDirection.Magnitude > 0 then
		Move:Play()
		StopAnim(Humanoid,'Hold')
		StopAnim(Humanoid,'Carrier')
	else
		StopAnim(Humanoid,'Move')
		Hold:Play()
	end
	-- Mouse --
	local MousePos = nil
	if Mouse.Hit.Position ~= MousePos then
		UserInputs.MouseBehavior = Enum.MouseBehavior.LockCenter
		HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position,Vector3.new(Mouse.Hit.Position.X,HumanoidRootPart.Position.Y,Mouse.Hit.Position.Z))
		MousePos = Mouse.Hit.Position
	end
	InModeTween:Play()
end

What I’m tryna do is loop it so depending on if the character is moving. The Move animation plays. If his standing the stand animation plays.

Do we know how to make if statements checking if character is standing or moving?

if Humanoid.MoveDirection.Magnitude > 0 then
thats what this lines for

I got it fixed but now when I try exiting QB Mode it locks me in and I cant exit
https://medal.tv/games/roblox-studio/clips/HwwiZjspyJvOp/d1337sP7ymhZ?invite=cr-MSxBMkosMTM4NjA2Mzg2LA

1 Like

I see here something else. Perhaps I’m hallucinating but I see that the standing animations keeps playing again and again when standing for a bit longer. Am I right?

No, Its like when I’m moving before I enter QB mode it plays the move animation and if I stop moving the move animation will keep and playing when I’m not moving. And its the same for the stand animation

Try using Humanoid.Running event instead of the loop.

Ive tried that it did worse… (char.20)

Did You put task.wait(1) in the loop or just task.wait()?

This is the code now

function QBOn()
	script.Mode.Enabled = true
	Character:FindFirstChild('FB'):SetAttribute('QBMode',true)
	task.spawn(function()
		while task.wait(0.1) and QBOn() do
		end
	end)
	if Humanoid.MoveDirection.Magnitude > 0 then
		Move:Play()
		StopAnim(Humanoid,'Hold')
		StopAnim(Humanoid,'Carrier')
	else
		StopAnim(Humanoid,'Move')
		Hold:Play()
	end
	-- Mouse --
	local MousePos = nil
	if Mouse.Hit.Position ~= MousePos then
		UserInputs.MouseBehavior = Enum.MouseBehavior.LockCenter
		HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position,Vector3.new(Mouse.Hit.Position.X,HumanoidRootPart.Position.Y,Mouse.Hit.Position.Z))
		MousePos = Mouse.Hit.Position
	end
	InModeTween:Play()
end

Try changing this:

if Humanoid.MoveDirection.Magnitude > 0 then
Move:Play()
StopAnim(Humanoid,'Hold')
StopAnim(Humanoid,'Carrier')
else
StopAnim(Humanoid,'Move')
Hold:Play()
end

to this:

if Humanoid.MoveDirection.Magnitude == 0 then
StopAnim(Humanoid,'Move')
Hold:Play()
else
Move:Play()
StopAnim(Humanoid,'Hold')
StopAnim(Humanoid,'Carrier')
--Move:Play()
end

P.S: also try commeting and uncommenting Move:Play() to check if the order of running this command matters.

1 Like

ill try it later I gotta do sum

It worked but the problem now is that I’m stuck in QB Mode. I cant get out

I will come back to this problem later. Consider showing a footage of the issue.

2 Likes

I had no time yesterday, I apologize.

1 Like

Your fine, Shall we continue? (char2.0)

1 Like

I don’t know, I don’t seem to have a lot of free time. I can only suggest with playing with a lot of printing commands to debug the code by yourself.

1 Like