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