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

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
        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

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

1 answer

Log in to vote
2
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
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!

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

Answer this question