How can I improve this old redundant code?

if TM.check() == "R" then
		Leg = "R"
		Tween.TweenWeld("Right Leg", CFrame.new(0.5,-2,0), CFRAME_HERE, Direction, Style, Duration)
	else
		Leg = "L"
		Tween.TweenWeld("Left Leg", CFrame.new(-0.5,-2,0), CFRAME_HERE_L, Direction, Style, Duration)
	end
	wait(0.3)
	if Leg == "R" then
		Tween.TweenWeld("Right Leg", CFRAME_HERE, CFrame.new(0.5,-2,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, Duration * 1.1)
	else
		Tween.TweenWeld("Left Leg", CFRAME_HERE_L, CFrame.new(-0.5,-2,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, Duration * 1.1)
	end

Is there any way I can make it so it doesnt check the leg twice after the .3 second delay, and instead find a way to make it better?

When I say that, I don’t mean like this:

if TM.check() == "R" then
		Leg = "R"
		Tween.TweenWeld("Right Leg", CFrame.new(0.5,-2,0), CFRAME_HERE, Direction, Style, Duration)
	    wait(0.3)  -- or task.delay whatever
        Tween.TweenWeld("Right Leg", CFRAME_HERE, CFrame.new(0.5,-2,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, Duration * 1.1)
	else
		Leg = "L"
		Tween.TweenWeld("Left Leg", CFrame.new(-0.5,-2,0), CFRAME_HERE_L, Direction, Style, Duration)
        wait(0.3)  -- or task.delay whatever
        Tween.TweenWeld("Left Leg", CFRAME_HERE_L, CFrame.new(-0.5,-2,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, Duration * 1.1)
	end

That 0.3 is not just an animation thing, it’s also used for some actual logic in the game, so I don’t want have to go through the extra trouble of adding a million task.wait(0.3)'s just to make the animations.

You could use a debounce in tandem with a Tween.Completed:Wait() event.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.