How do I fix this Function Order?

How do I fix this?

image

While the Open Function is running, I want the Robbed Function to Run if the block is touched and stop the Open function from running. But if the 5 seconds is up, I want the Closed function to run. I want the entire things to be a loop! How do I do this?

This is the code:

while true do
Open()
script.Parent.Touched:Connect(Robbed)
wait(5)
Closed()
wait(5)
end

Can you please provide the Open() and Close() functions?

Sure. U can soon! ———————————-

Here’s the entire script:

ocal Player = game.Players.LocalPlayer
local Alarm = game.ReplicatedStorage:WaitForChild("Alarm")
local Tele = game.ReplicatedStorage:WaitForChild("BankTele")

local function Open()
	Alarm.Parent = workspace
	Alarm.Base.Alarm.Playing = true
	Tele.Parent = game.ReplicatedStorage
	Alarm.Parent = game.ReplicatedStorage
	Alarm.Base.Alarm.Playing = false -------------- Open Function
	return
end

local function Robbed()
	Alarm.Parent = workspace
	Alarm.Base.Alarm.Playing = true
	return
end

local function Closed()
	Alarm.Parent = game.ReplicatedStorage ----- Closed Function
	Alarm.Base.Alarm.Playing = false
	Tele.Parent = game.Workspace
	script.Parent.CanTouch = false
	script.Parent.CanCollide = false
	return
end

while true do
	Open()
	script.Parent.Touched:Connect(Robbed)
	wait(5)
	Closed()
	wait(5)
end

script.Parent.Touched:Connect(Robbed)
ocal Player = game.Players.LocalPlayer
local Alarm = game.ReplicatedStorage:WaitForChild("Alarm")
local Tele = game.ReplicatedStorage:WaitForChild("BankTele")

local isOpen = true

local function Open()
	isOpen = true

	Alarm.Parent = workspace
	Alarm.Base.Alarm.Playing = true
	Tele.Parent = game.ReplicatedStorage
	Alarm.Parent = game.ReplicatedStorage
	Alarm.Base.Alarm.Playing = false -------------- Open Function
	return
end

local function Robbed()
	if not isOpen then
		return
	end

	Alarm.Parent = workspace
	Alarm.Base.Alarm.Playing = true
	return
end

local function Closed()
	isOpen = false

	Alarm.Parent = game.ReplicatedStorage ----- Closed Function
	Alarm.Base.Alarm.Playing = false
	Tele.Parent = game.Workspace
	script.Parent.CanTouch = false
	script.Parent.CanCollide = false
	return
end

while true do
	Open()
	script.Parent.Touched:Once(Robbed)
	wait(5)
	Closed()
	wait(5)
end

Well if the loop continues then the Script. Parent part would have a lot of touched events (which I assume isn’t your goal)
Here is how you should construct your loop


script.Parent.Touched:Connect(Ropped) -- you define the event before the loop otherwise it won't work

while true do
    Open() 
    task.wait(5)
    Close()  
    task.wait(5)
end

You can do this:

local States = {Open, Closed} -- store functions here

local i = 0 -- to keep track of what function out code will fire

while true do
    i = (i % 2) + 1 -- this will reset the number back to 0 if the number is two,
    -- but we are adding + 1 to ensure that the index inside the table can be
    -- used to Avoid Errors
    
    States[i]() -- calls function from table based on index given
    task.wait(5) -- yields for "almost" 5 seconds
end
1 Like

How would I fix this?

while true do
	Open() 
	if script.Parent.Touched then
		Robbed()
	end
	task.wait(5)
	Closed()  
	task.wait(5)
end

script.Parent.Touched is not a boolean (true/false). It is an event.

Thank you for the tip, I think it will work!

Wait, I tried it, and I don’t think I knew how to program that. Can you see if I did anything wrong in this script? Because It’s not working right:

All these scripts are contained in one part:

local Player = game.Players.LocalPlayer
local Alarm = game.ReplicatedStorage.Alarm
local Tele = game.ReplicatedStorage.BankTele
local CanRob = script.Parent.CanRob.Value

while true do
	wait()
	if CanRob == true then
		Alarm.Parent = workspace
		Alarm.Base.Alarm.Playing = true
		Tele.Parent = game.ReplicatedStorage
	elseif CanRob == false then
		Alarm.Parent = game.ReplicatedStorage ----- Closed Function
		Alarm.Base.Alarm.Playing = false
		Tele.Parent = game.Workspace
		script.Parent.CanTouch = false
		script.Parent.CanCollide = true
	end
end

script 2:

local Player = game.Players.LocalPlayer

local Alarm = game.ReplicatedStorage.Alarm

local Tele = game.ReplicatedStorage.BankTele

local CanRob = script.Parent.CanRob.Value

script.Parent.Touched:Connect(function(Hit)

if Hit.Parent:FindFirstChild(“Humanoid”) then

CanRob = true

end

end)