Which way should i break this loop?

Hi, i’ve been trying to make my gui color changing script break a loop, and the loop is used to change the colors, looping through colors, like a rainbow sort of, and the problem i’m having is i don’t know how i should break the loop.

Script

The script is in ServerScriptService.

 local ReplicatedStorage = game.ReplicatedStorage

local ColorChangeBlack = ReplicatedStorage:WaitForChild("ColorChangeBlack")
local ColorChangeRed = ReplicatedStorage:WaitForChild("ColorChangeRed")
local ColorChangePink = ReplicatedStorage:WaitForChild("ColorChangePink")
local ColorChangePurple = ReplicatedStorage:WaitForChild("ColorChangePurple")
local ColorChangeBlue = ReplicatedStorage:WaitForChild("ColorChangeBlue")
local ColorChangeGreen = ReplicatedStorage:WaitForChild("ColorChangeGreen")
local ColorChangeYellow = ReplicatedStorage:WaitForChild("ColorChangeYellow")
local ColorChangeOrange = ReplicatedStorage:WaitForChild("ColorChangeOrange")
local ColorChangeWhite = ReplicatedStorage:WaitForChild("ColorChangeWhite")
local ColorChangeTeal = ReplicatedStorage:WaitForChild("ColorChangeTeal")
local ColorChangeToothpaste = ReplicatedStorage:WaitForChild("ColorChangeToothpaste")
local ColorChangeRainbow = ReplicatedStorage:WaitForChild("ColorChangeRainbow")

ColorChangeBlack.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(0,0,0) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeRed.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(255,0,0) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangePink.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(255, 0, 191) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangePurple.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromRGB(123, 0, 123) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeBlue.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(0,0,255) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeGreen.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(0,255,0) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeYellow.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(255,255,0) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeOrange.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromRGB(213, 115, 61) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeWhite.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.new(255,255,255) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeTeal.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromRGB(0, 143, 156) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeToothpaste.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
	player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromRGB(0,255,255) -- This line uses the the player object returned by the OnServerEvent
end)
ColorChangeRainbow.OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
		while true do
	 for a = 1,360,1 do wait(.05)
	  player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromHSV(a/360,1,1) -- Changes colors
	
	 end
	end	 -- This line uses the the player object returned by the OnServerEvent
end)

Details

  • The point of the script is to have RemoteEvents which are made for each color, which are fired on server from the client localScript when activated, and using them to change colors of a gui.

What would be a good way to do this?

I thought about breaking the loop when another RemoteEvent was fired, but i couldn’t find any functions for it, and i can’t think of many other ways, so i don’t know how i should do this.

Thanks, all help is appreciated!

1 Like

Unrelated, but please consider condensing all of these remotes. Using a parameter to determine the color would work fine and be more organized as opposed to a different remote for each color!

With that aside, what you’re asking can be done using the break command, built into lua, which will terminate a loop when used. Essentially…

  1. Have a remote set a variable to true
  2. Inside the while true do loop, check the value of that loop.
  3. If it is true, set it to false and use the break command.

Sample code:

SomeRemote.OnServerEvent:Connect(function(...)
    while true do
        -- Put your loop code here
        if variable == true then
            variable = false -- So that it can be cancelled again in the future
            break -- Ends the while true do loop entirely.
        end
    end
end)

SomeOtherRemote.OnServerEvent:Connect(function(...)
    variable = true
end)
1 Like
while true do
          for a = 1,360,1 do wait(.05)
	            player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromHSV(a/360,1,1)
	  end
          break -- insert break here
end

Also Thunder is right, you should really condense your code as it’s really hard to read and, correct me if I’m wrong, save on computing power.

ColorChangeRainbow. OnServerEvent:Connect(function(player,activated) -- The first parameter is always the player object
    while true do
        for a = 1,360,1 do wait(.05)
            player.Character.Head.TitleGUI.TitleLabel.TextColor3 = Color3.fromHSV(a/360,1,1) -- Changes colors
	    end
     return
   end	
end)