Is their anyway to clean up this code?

I’m trying to implement a slide feature into this code but it’s too messy.

UIS.InputBegan:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.LeftShift and gpe == false then
sprinting = true

	while sprinting and power >= 0.2 do
		wait()
		if character.Humanoid.MoveDirection.Magnitude > 0 and sprinting then
		Tween1:Play()
		character.Humanoid.WalkSpeed = Run
		power = power - 0.03
			Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
		else
			tween2:Play()
			character.Humanoid.WalkSpeed = Walk
			power = power + 0.01
				Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
		end
	end
end

end)

UIS.InputEnded:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.LeftShift and gpe == false then
sprinting = false
while sprinting == false and power < 10 do
wait()
tween2:Play()
character.Humanoid.WalkSpeed = Walk
power = power + 0.01
Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
end
end
end)
This code is for a stamina bar.

I don’t think that much can be done to make the code less messy. Actually it’s not that messy. I tried making it simpler and I think it’s better for a bit. I replaced wait function with task.wait and I moved this function to while loop’s conditions, which is better. Here’s what I did:

UIS.InputBegan:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftShift and gpe == false then
		sprinting = true
		while sprinting and power >= 0.2 and task.wait() do
			if character.Humanoid.MoveDirection.Magnitude > 0 and sprinting then
				Tween1:Play()
				character.Humanoid.WalkSpeed = Run
				power = power - 0.03
				Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
			else
				tween2:Play()
				character.Humanoid.WalkSpeed = Walk
				power = power + 0.01
				Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gpe)
	if input.KeyCode == Enum.KeyCode.LeftShift and gpe == false then
		sprinting = false
		while sprinting == false and power < 10 and task.wait() do
			tween2:Play()
			character.Humanoid.WalkSpeed = Walk
			power = power + 0.01
			Bar.Size = UDim2.new(power/ 10, 0, 1, 0)
		end
	end
end)
2 Likes

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