Hello, I just wanted to know what this script does in a model I found, so I can try and make something simular on my own. If you could add a comment to each line it would be appreciated. Thanks, Jhunlzeus4.
function OpenDoors() door.AccessGranted:play() for i,v in pairs (DoorModel.Lights:GetChildren()) do v.brickColor = BrickColor.new("Lime green") end door.DoorOpen:play() for i=0,math.pi*2,math.pi/30 do r.Stepped:wait() door.CFrame=ret+Vector3.new(2.5,0,2.5)*look-math.cos(i)*2.5*look door1.CFrame=ret1-Vector3.new(2.5,0,2.5)*look+math.cos(i)*2.5*look if string.format("%3.2f",i)=="3.14" then door.Transparency=1 door1.Transparency=1 wait(3) door.Transparency=Original door1.Transparency=Original1 door.DoorClose:play() end end door1.CFrame = ret1 door.CFrame = ret for i,v in pairs (DoorModel.Lights:GetChildren()) do v.brickColor = BrickColor.new("Institutional white") end end
door.AccessGranted:play()
-- AccessGranted is evidently a sound. Note that play
is deprecated; use Play
instead.
for i,v in pairs (DoorModel.Lights:GetChildren()) do v.brickColor = BrickColor.new("Lime green") end
Assigns each child of "Lights" the BrickColor Lime green. Note that it should be v.BrickColor
(brickColor
is deprecated).
for i=0,math.pi*2,math.pi/30 do
math.pi*2 is 360 degrees, so this for loop is rotating the doors 360 degrees in increments of 6 degrees (math.pi/30 == 180 degrees /30 == 6 degrees)
r.Stepped:wait()
'r' must be the RunService. Stepped
runs 2x as often as wait()
. :wait()
is deprecated, use :Wait()
door.CFrame=ret+Vector3.new(2.5,0,2.5)*look-math.cos(i)*2.5*look door1.CFrame=ret1-Vector3.new(2.5,0,2.5)*look+math.cos(i)*2.5*look
I'd need to see more of the script to explain this properly, though it's clearly doing rotation. Another way of rotating something is to use CFrame.fromAxisAngle(Vector3.new(0, 1, 0), angle)
, which you would then add to the door's position so that it stays in place. You have to do a bit more work if you want the door to rotate on its edge rather than around its centre.
if string.format("%3.2f",i)=="3.14" then
This is an interesting way of determining whether the door is half-way through its rotation. A better way might be to extract the CFrame code into a function and then use two for
loops separated by the contents of this if
block:
local function rotate(angle) --CFrame code here end local step = math.pi/30 for i = 0, math.pi, step do r.Stepped:Wait() rotate(i) end rotate(math.pi) -- this line may not be needed --play sounds, wait(3), etc here for i = math.pi, math.pi*2, step do r.Stepped:Wait() rotate(i) end
After the for loop is done, the function returns the doors to their previous state (including lights) -- setting the light colour should really be its own function, by the way.