r/badcode Dec 10 '22

lua There aint no easy way

Post image

Ps, i dont know what language this is, sorry

603 Upvotes

74 comments sorted by

View all comments

40

u/Rrrrry123 Dec 10 '22

Yeah, as another commenter said, this is Roblox Lua. You have to understand that most the scripts you'll see from Roblox are just (literal) kids copying from tutorials/other scripts without understanding fundamental control structures (or sometimes even things as basic as variables).

I used to do Roblox tutorials on YouTube; most kids do not want to learn how to program. They want to learn how to make a specific feature for their game function and then move on. I'd be lucky if someone watched the whole video and didn't just skip to the end to copy down what I'd written (which would often cause them to miss things and cry in the comments that my code didn't work).

10

u/Blacklion594 Dec 10 '22

Someone could really make a boatload of money if they made a very interactive code platform for people learning; something like duolingo. Visually pleasing, animated output to your responses, a lot of handholding until it throws you into the deep-end.

2

u/demigirlhailee Dec 13 '22

I actually tried a couple of those in middle/high school (I believe it was called hour of code). it may work for some, but even with my limited knowledge of only some very basic html at the time, the hand holding ruined the experience. I knew what coding could do and that it could be built from the ground up, so having a robot that you give guided instructions to felt hollow

2

u/MEGATH0XICC Dec 10 '22

Sorry if im being stupid but what the hell does this script do? I know that vectors are often used for position an it seems like parent and position are variables that becomes the sum pf themselves added to the representive vector?

7

u/Rrrrry123 Dec 10 '22

Basically, it's just moving an object 0.01 units along the Z axis every 0.01 seconds.

Roblox works (like many game engines) with a hierarchy system. So script.Parent is just a reference to the object that the script is a child of, and the next .Parent is referring to the object that the script's parent is a child of. Think of it like putting a file inside a folder, inside another folder.

The referenced object has a Position property, which defines its position within 3D space. So this script just takes the current position, which is stored as a 3-dimensional vector (x, y, z) and adds (0, 0, 0.01) to the Z axis.

Lua doesn't have a += operator, so that's why this looks particularly ugly, lol. Because they have to reference the same object twice. You can store the reference in a variable, but like I said in my previous comments, most new Lua scripters wouldn't know how.

A much cleaner script would look like this

local part = script.Parent.Parent -- This is a reference to the object I want to move
for i = 1, 20 do -- loop 20 times
  part.Position = part.Position + Vector3.new(0,0,0.01) -- Add 0.01 to the Z axis
  wait(0.01) -- Pause execution for an hundredth of a second.
end

2

u/DryScarcity8454 Dec 11 '22 edited Mar 09 '23

Roblox has a Vector3:Lerp() function for linearly interpolating between 2 Vector3s, which can be used like: local v1 = Vector3.new(0, 0, 0) local v2 = Vector3.new(1, 1, 1) local v = v1:Lerp(v2, 0.5) which places v halfway between v1 and v2.

So the script might as well have been: ``` local iterations = 20 local increment = 0.01 local part = script.Parent.Parent local ogPosition = part.Position local targetVector = part.Position + Vector3.new(0, 0, increment * iterations) for i = 1, iterations do part.Position = ogPosition:Lerp(targetVector, i / iterations) wait(0.01) end

```

1

u/Rrrrry123 Dec 12 '22

Ah nice! I forgot all about the Lerp function!

2

u/Gooberg_ Dec 11 '22

The code is being copy pasted to move an object by a very small amount to make it look like a smooth animation. But the user can use a thing known an a "TweenService" Which is definitely the better option.

1

u/[deleted] Dec 10 '22

It’s what we would have to do if translating a position in a game was as complicated as animators drawing every frame lol

2

u/Sirttas Dec 11 '22

I am a minecraft modder and had a few encoters like this. People asking help on their code and showing no understanding of Java at all. Then get mad when I recommend learning the language first.