How do I make the script repeat itself automatically?

Hello! I have a simple script here, under the influence of which the cold-gray part turns into green neon, and when you click on it, the red neon is rendered.

And I want the consequences of this scenario to be repeated endlessly, that is, automatically, without the need to reproduce this action manually.

So that when you click on the red neon part, it does not remain so, but turns green again, as before, and when you press it again, it turns red again and so endlessly.

Could you please tell a beginner how he can implement such a looped causal relationship in his game?

Now that I have explained to you what I am trying to achieve and what I am asking for help with, namely to add a few necessary lines, let me acquaint you with this script:

local SB = game.Workspace.SquareButton
SB.BrickColor = BrickColor.Green()
SB.Material = Enum.Material.Neon
SB.Position = Vector 3.new(-179.5, 0.5, 37.5)
SB.Anchored = true
function Change()
local P = script.Parent
P.BrickColor = BrickColor.Red()
P.Material = Enum.Material.Neon
P.Position = Vector 3.new(-179.5, -0.4, 37.5)
end
script.Parent.ClickDetector.MouseClick:connect(Change)

1 Like
local SB = game.Workspace.SquareButton
SB.BrickColor = BrickColor.Green()
SB.Material = Enum.Material.Neon
SB.Position = Vector 3.new(-179.5, 0.5, 37.5)
SB.Anchored = true
function Change()
    SB.BrickColor = BrickColor.Green()
    SB.Position = Vector 3.new(-179.5, 0.5, 37.5)
    local P = script.Parent
    P.BrickColor = BrickColor.Red()
    P.Position = Vector 3.new(-179.5, -0.4, 37.5)
end

script.Parent.ClickDetector.MouseClick:connect(Change)
1 Like

Maybe try to use something like that ? A while loop with a wait. Sorry the script is kinda bad

function Change()
	while(5)  do -- wait 5s do
		local P = script.Parent
		P.BrickColor = BrickColor.Red()
		P.Material = Enum.Material.Neon
		P.Position = Vector 3.new(-179.5, -0.4, 37.5)
		wait(2) -- wait 2s
		local SB = game.Workspace.SquareButton
		SB.BrickColor = BrickColor.Green()
		SB.Material = Enum.Material.Neon
		SB.Position = Vector 3.new(-179.5, 0.5, 37.5)
		SB.Anchored = true
	end
end

script.Parent.ClickDetector.MouseClick:connect(Change)
1 Like

yea i was going say that to haha
heres how i would do it

function NeonLoop()
    SB.BrickColor = BrickColor.Green()
    SB.Position = Vector3.new(-179.5, 0.5, 37.5)
    SB.Postion = Vector3.new(-179.5, -0.4, 37.5)
    SB.BrickColor = BrickColor.Red()
end

script.Parent.ClickDetector.MouseClick:connect(NeonLoop)
1 Like

Of course. I will definitely try your suggested ways to solve this problem. Only I will need some time for this, but I am sure that her solution is already here…