How do I repeat this script

Im trying to make this script repeatable so it can run multiple times.

local Light = game.Workspace.Views.ViewRight.SpotLight
local Sun = game.Workspace.Light.PointLight2
local Script = game.Workspace.Monitor.Screen.SurfaceGui.Frame.Generator
local Label =  game.Workspace.Monitor.Screen.SurfaceGui.Generator.TextLabel
local Gen = game.Workspace.Monitor.Screen.SurfaceGui.Generator
local Button = game.Workspace.Monitor.Screen.SurfaceGui.Generator.TextButton2


wait(10)
if Light.Enabled == true then
	Light.Enabled = false
	Sun.Enabled = false
	Script.Visible = true
	
	local TweenService = game:GetService("TweenService") 
	local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
	local Tween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(0, 0, 0)})
	Tween:Play()
	
end

	


Button.MouseButton1Click:Connect(function()
	
	if Gen.Visible == true then
		wait(5) 
		Label.Text = "Generator Online"
		Light.Enabled = true
		Sun.Enabled = true
		Button.Visible = false
		Script.Visible = false
		local TweenService = game:GetService("TweenService") 
		local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Tween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(70, 70, 70)})
		Tween:Play()

		end
	end)





How do I repeat a script like this?

Can’t you just use a while loop, repeat loop or a for loop.

Tried to add while true do at the start of the script.

Ended up breaking the script.

You will have to do something like this:

while wait() do
   --insert code here
end

It must be because you have to wait for the Tween to end before repeating by using TweenBase.Completed.

Make it into a function after that you can do a while loop or a repeat until

Setup function to reset button to initial state so after clicking it will activate then wait 10 seconds and reset so it can be activated again as below you could call the reset function with spawn or routine but this is basic setup

function   ResetButton()
	wait(10)
	if Light.Enabled == true then
		Light.Enabled = false
		Sun.Enabled = false
		Script.Visible = true
		
		local TweenService = game:GetService("TweenService") 
		local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Tween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(0, 0, 0)})
		Tween:Play()
	end
end

ResetButton()  --   add this for Initial setup when script runs

Button.MouseButton1Click:Connect(function()
	if Gen.Visible == true then
		wait(5) 
		Label.Text = "Generator Online"
		Light.Enabled = true
		Sun.Enabled = true
		Button.Visible = false
		Script.Visible = false
		local TweenService = game:GetService("TweenService") 
		local info = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
		local Tween = TweenService:Create(game.Lighting, info, {Ambient = Color3.fromRGB(70, 70, 70)})
		Tween:Play()
	end
    ResetButton()  -- add this to call function after button was turned on
end)

Then when you are executing the function do coroutine.warp(ResetButton()). It will ignore the wait for the tween finishing.

1 Like

could add a spawn to it also if he needed

That could also work too. I am just used to coroutines.

1 Like

Added the functions to the script, Its repeating now.

1 Like