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

I want to make a door close every 5 seconds in case it was left open for too long but it wont work?

Asked by 8 years ago
while true do
    wait (5)
    script.Parent.PortcullisGate.Open.Value = false
end

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

One of our problems is that while loops are for when we are always using them. For example

while wait(1) do
    print("A second has passed!")
end

But, in really rare cases, are we ever going to use our door constantly? Your answer should be a no most of the time. We can reduce the unnecessary code running by doing a function. Think of a function as a constant checker that does not make lag. One way we can use functions is when a player joins! We don't need to search for a new player every second, and that is ridiculous! So we can use the PlayerAdded function.

game.Players.PlayerAdded(function(plr)
    print(plr.Name.." has joined!")
end)

Some ways to find things about things is using the Object Browser. Open it by going to view and clicking "Object Browser". Now that we scrolled down, we see a thing called "Changed" with a little lightning picture beside it. That means that "Changed" is a event. Also, in the description it says "Fired after a property changes value. The property argument is the name of the property", and that tells us a bit what and when the event fires. The things with blue bricks are properties, and the purple bricks are what you can do with colons like :FindFirstChild(). Now that we know how to use the Object Browser, lets jump straight into the event. First, we need to get the location of the thing that we want it to detect changes. The rest is just the code you want running.

local deb = false --setting up a debounce
script.Parent.PortcullisGate.Open.Changed(function(PC) --PC is what we are going to use for "Property Changed.
if PC == "Value" and deb == false then 
    deb = true
    wait(5)
    deb = false
    if script.Parent.PortcullisGate.Open.Value = true then script.Parent.PortcullisGate.Open.Value = false end
    end
end)
0
As much as I want this to work, it didn't. Thank you for writing all of this though greybird700 63 — 8y
0
Maybe you could join a team create and see if you can fix it greybird700 63 — 8y
0
just use my advce. go into Object Browser SH_Helper 61 — 8y
Ad

Answer this question