Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone quickly explain a script to me?

Asked by 6 years ago

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
0
It will open the door for you while animating it and will make lights on the door turn white... There you go, you have it all greatneil80 2647 — 6y
0
Yes, but how does it make the door move? I want to understand how it works and how changing which argument would do what, jhunlzeus4 20 — 6y
1
The door moves using CFrame - which is quite an advanced concept. I can barely understand this code properly myself. There are some resources on the wiki on how CFrame works. The CFrame gets changed every time it loops, which is shown by the for loop. We cannot teach you exactly how everything works - you'll need to read up on it and understand it all yourself. TheDeadlyPanther 2460 — 6y
0
it moves by using CFrame and Vector3.new and if it is a round door it will use pi because pi works for circles greatneil80 2647 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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.

0
Thanks, now that it is explained it makes better sense jhunlzeus4 20 — 6y
Ad

Answer this question