How would I be able to stop the color from changing after deselecting?

I’m currently working on a nametag color system and have a rainbow color as one of the options that changes colors. If you try to change colors it will just continue without switching colors. I’ve tried making it so after deselecting it, the colors changes to white but that didn’t seem to work either. I’m using Topbar+ for the buttons.

Code:

game.ReplicatedStorage.System.CRainbow.OnServerEvent:Connect(function(p)
    while wait() do
        for i = 1, 255 do
        p.Character.Rank.Frame.Name1.TextColor3 = Color3.fromHSV(i/255, 1, 1)
        wait(.1)
        end
    end
end)
    Icon.new()
    :setEnabled(canViewIcon)
    :setLabel("Rainbow")
    :bindEvent("selected", function(icon)
        game.ReplicatedStorage.System.CRainbow:FireServer()
    end)
    :bindEvent("deselected", function(icon)
        game.ReplicatedStorage.System.CWhite:FireServer()
    end)
})

Can you show the CWhite event function?

Also, the While wait() loop is infinite, so you should add break when the player deselects the nametag.

1 Like

CWhite is basically just another color you can change to

game.ReplicatedStorage.System.CWhite.OnServerEvent:Connect(function(p)
	p.Character.Rank.Frame.Name1.TextColor3 = Color3.fromRGB(255, 255, 255)
end)

The loop is in a separate script than the script with the deselect in it so how would I use break?

There are a couple of ways you can do that, but I think the easiest one is creating a bool value and when it turns false it breaks the loop.

tagOn = false

game.ReplicatedStorage.System.CRainbow.OnServerEvent:Connect(function(p)
	tagOn = true
	while tagOn do
		wait()
		for i = 1, 255 do
			p.Character.Rank.Frame.Name1.TextColor3 = Color3.fromHSV(i/255, 1, 1)
			wait(.1)
		end
	end
end)

game.ReplicatedStorage.System.CWhite.OnServerEvent:Connect(function(p)
	tagOn = false
	wait()
	p.Character.Rank.Frame.Name1.TextColor3 = Color3.fromRGB(255, 255, 255)
end)
1 Like