#Is this script good for a moving part?

1 messages · Page 1 of 1 (latest)

humble mulch
#

I'm a beginner and just learned about types of loops. I feel like this is a good start but I would appreciate feedback on it!

local part = script.Parent

while true do
    for i = 1, 100 do
        if i >= 51 then
        part.Position = part.Position + Vector3.new(0, -0.01, 0)
        wait(0.01)
        print(i)
        else
        part.Position = part.Position + Vector3.new(0, 0.01, 0)
        wait(0.01)
        print(i)
        end
    end
end
stiff dragonBOT
#

studio** You are now Level 2! **studio

worthy stone
#

look into runservice renderstepped for smooth motionThumbs

#

or tweenservice

vapid ibex
#

yes

rocky imp
# humble mulch I'm a beginner and just learned about types of loops. I feel like this is a good...

this is an acceptable loop for your experience level, it's not perfect but nobody makes perfect code when they're still learning

get rid of those print statements unless they're actively being used for debugging, it's just cleaner and less laggy

i would recommend getting into the habit of using task.wait() over wait(), for most applications it doesn't matter but using task.wait() is generally considered better practice because it performs better. they're identical otherwise.

also, it's a matter of choice mostly, but if you want to type slightly less while modifying a value, x = x + 1 is identical to x += 1, you could write part.Position += Vector3.new(0, 0.01, 0) instead.

discussing implementations now, it's generally better to use tweenservice or a bodyposition over changing position in a loop like this. tweenservice is used for anchored parts, while bodypositions are used for unanchored parts. bodypositions use roblox's velocity system, and that allows them to carry other parts (this is especially important if it's something like a platform the player is meant to ride on!!!), while anchored parts would require a custom carrying implementation to have the same effect.

earnest vapor
#

also most people use task.wait(time) instead of ``wait(time)` because its more accurate

brittle bramble
#

as they said, your code is reasonable but letting roblox handle the movement physics by using tw ser and bp makes movement way smoother

raven eagle
#

focus on working. do not worry about performance.

#

do not worry about best practices.