Still have questions? Join our Discord server and get real time help.
Log in to vote

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

Asked by 3 months ago

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

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

bruh math.ceil is just rounding TheluaBanana 946 — 3mo
also i think there is a lerp function for that TheluaBanana 946 — 3mo
start * (1 - t) + end * t TheluaBanana 946 — 3mo
where t is a number between 0 to 1 TheluaBanana 946 — 3mo
View all comments (3 more)
and then just iterate that TheluaBanana 946 — 3mo
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. TheluaBanana 946 — 3mo
for that i would recommend u look up roblox lerping TheluaBanana 946 — 3mo

1 answer

Log in to vote
Answered by 3 months ago
Edited 3 months ago

Here is what it means:

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

local num = 0.5

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

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!

thank you i could tell this took a long time :) it really helped! botw_legend 452 — 3mo
No worries! Lightning_Game27 218 — 3mo

Answer this question