Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

while true do
	wait (5)
	script.Parent.PortcullisGate.Open.Value = false
end

1 answer

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)

3 comments

0

As much as I want this to work, it didn't. Thank you for writing all of this though

0

Maybe you could join a team create and see if you can fix it

0

just use my advce. go into Object Browser

This discussion is preserved as it was. New answers, comments, and votes are closed.