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

Understanding a gate script. How to change a lever to a button?

Asked by 9 years ago

I'm trying to adapt a gate script from one I've got from free models, the current script works by having the player click a lever which then makes the lever handle change direction and the gate open. However I want to change it so that the gate opens by a button and not by a lever.

Here is the script for the lever, I'm not sure what part to remove to just have it open by a simple button click, is it possible somebody could explain all the parts of this script for me?

local divs = 32 local time = 0.5 local dist = math.pi / 2

local progress = 0 local direction = 1

function positionLever(angle) local base = script.Parent.LeverBase.CFrame local transform = CFrame.fromEulerAnglesXYZ(angle, 0, 0) * CFrame.new(0, 1, 0)

script.Parent.Lever.CFrame = (base * CFrame.new(0, 0.5, 0)) * transform

end

local ang = math.pi / 4 positionLever(ang) script.Parent.Lever.ClickDetector.MouseClick:connect(function() if progress ~= 0 then return end

direction = -direction

while true do
    if progress >= divs then
        script.Parent.UpDown.Value = not script.Parent.UpDown.Value
        progress = 0 
        break 
    end

    progress = progress + 1

    ang = ang + ((dist / divs) * direction)
    positionLever(ang)

    wait(time / divs)
end

end)

All I need the script to do is that when a button is click it activates a parent script:

script.Parent.Lever.UpDown.Changed:connect(function() script.Parent.PortcullisGate.Open.Value = true end)

Which I believe then activates another script which causes the model PortcullisGate to open.

1 answer

Log in to vote
0
Answered by 9 years ago

Well, for one I will just give you a gate script. You can edit it however you want, I do not care.

First Script here is the button that will open the gate. You can change the time, and the names of the parts if you wish (depending on if the parts are renamed)

Door = script.Parent.Parent.Door ---- Tells the script that Door is script.Parent.Parent.Door, which shortens it, instead of writing script.Parent.Door everytime.

function onClicked() ----function
Door.Transparency = 1 ----Makes the door transparent, making it disappear.
Door.CanCollide = false ----Makes it so you can't collide with the door. This is how you can pull a car in.
end
script.Parent.ClickDetector.MouseClick:connect(onClicked) ----Runs the function when clicked.

Now, here's the closing script for the door.

Door = script.Parent.Parent.Door ---- Tells the script that Door is script.Parent.Parent.Door, which shortens it, instead of writing script.Parent.Door everytime.

function onClicked() ----function
Door.Transparency = 0 ----Makes the door visible, allowing the player see it.
Door.CanCollide = true ---- Makes it so you can collide with the door. This is how you can't enter the garage.
end
script.Parent.ClickDetector.MouseClick:connect(onClicked) ----Runs the function when clicked.

These scripts took me 3 seconds to create. Literally. They're that easy. Make sure to put the buttons INSIDE of the model, and edit the script to fit the location and name of the Door part. Hope this helped.

Ad

Answer this question