Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

i need help understanding some code (making a part move between two places)?

so this is the code

while true do
    if math.ceil((script.Parent.part.Position-script.Parent.anchor.Position).Magnitude) >= math.abs(script.Parent.part.PrismaticConstraint.TargetPosition) then
        script.Parent.part.PrismaticConstraint.TargetPosition = -script.Parent.part.PrismaticConstraint.TargetPosition
        wait(1)
    end
    wait()
end

it makes a part slide back and forth between two places and i want to understand how it works because im confused by the if math.ceil line and the script.parent line

7 comments

0

bruh math.ceil is just rounding

0

also i think there is a lerp function for that

0

start * (1 - t) + end * t

0

where t is a number between 0 to 1

0

and then just iterate that

0

that isnt how this specific script works and i apologise for not explaining that. I think it relies on some built in mechanic tho(i cant confirm this, as i am neither good at constraints nor at maths), and not just pure maths.

0

for that i would recommend u look up roblox lerping

1 answer

✓ Accepted answer

Here is what it means:

math.ceil means that it rounds up to the nearest whole number. For example:

local num = 0.5
print(math.ceil(num))

The above would print out 1 as 1 is the nearest whole number when rounding up.

math.abs means that it gives the absolute value of the number. For example:

local num = -0.5
print(math.abs(num))

The above would print out 0.5 as 0.5 is the absolute value of -0.5. Simple math lol

script.Parent means that the script in which you are writing will find the parent of it. For example:

If you add a part and add a script inside a part and type the following code:

script.Parent.Transparency = 0.5

Then you will see that the part in which the script you just wrote is in will become half-transparent as the parent of the script is the part.

So what this script means is:

while true do -- A constant loop
    if math.ceil((script.Parent.part.Position-script.Parent.anchor.Position).Magnitude) >= math.abs(script.Parent.part.PrismaticConstraint.TargetPosition) then [[--If the position of the part minus the position of the anchor, when rounded up is greater than or equal to the absolute value of the target position of the PrismaticConstraint in the part, then--]]
        script.Parent.part.PrismaticConstraint.TargetPosition = -					script.Parent.part.PrismaticConstraint.TargetPosition [[--The TargetPosition of the PrismaticConstraint will be equal to the negative value of the TargetPosition of the PrismaticConstraint--]]
        wait(1) -- Wait for 1 second
    end -- end the if statement
    wait() -- wait
end -- end the while true loop

Hopefully this helped! Please mark it as an answer if it helped as it took a lot of time to write!

2 comments

0

thank you i could tell this took a long time :) it really helped!

0

No worries!

This discussion is preserved as it was. New answers, comments, and votes are closed.