Spaghetti code improvements

Completely re-wrote this entire section of code by implementing guard and saved a bunch of lines while making it easier to read. Any tips on how to further squash it down?

Started with this bad boy
running

Turned it into this and saved a dozen lines

Edit: I changed line 101’s if statement to

if crouching or not sprinting then

and 106 to

if not fwd then return end

as the speed wouldn’t change from 1 to a normal walking speed, lol

Edit 2: Thanks for the input, was able to reduce it further down to this

4 Likes

You could condense the if statements into one

Nvm, I didn’t see they had code in them ignore what I said.

3 Likes

You could add ternary when you’re setting hum.WalkSpeed, idk if that would make it smaller.

1 Like

In the sprinting section you can do this:

if not sprinting and hum.WalkSpeed ~= speedTarget.mid then
	hum.WalkSpeed += if hum.WalkSpeed < speedTarget.mid then .25 else -.25;
	return
end

Another thing, in the not moving() part you can do:

if not moving() and hum.WalkSpeed ~= 1 then
...
return end

Also, there’s no need for the if hum.WalkSpeed == speedTarget.max line in 107 since there’s nothing after that.

4 Likes

I didn’t even know you could write something like that o.O

1 Like

Oh, it returns out of that if statement so the game doesn’t keep checking afterwards if the speed is above or below the max.

Yeah that’s what I was talking about lol that’s ternary.

I know about ternary, I just didnt know you could use it doing math operations like that

55 += if X then Y else Z

Pretty freaky

Have a look into finite state machines.

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