While True Do Loop help

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make a button that will make the map night and make lamps start to Disco. ( Change Color )

  1. What is the issue? Include screenshots / videos if possible!

It keeps returning as an Error: The current identity (2) cannot settings() (lacking permission 1) ( Attempting to change the timeout length since I got an error about timeout. )

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I haven’t been able to figure out why it’s erroring.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local CD = script.Parent.ClickDetector
local t = game.Lighting.ClockTime 
local part = script.Parent
local ws = game.StarterGui.Warning.Frame
local time1 = 0
local complete = 30

settings().Studio.ScriptTimeoutLength = -1

for _, v in pairs (game.Workspace:GetDescendants()) do
	
	if v.Name["Meshes/lamba_Cube.004"] then 
		
		wait(5)
		
		function onMouseClick()
			while true do
				part.Name = BrickColor.new(math.random(0.1,1), math.random(0.1,1), math.random(0.1,1))
				wait(.1)
				time1 = time1 + .1
				
				if time1 == complete then
					break
				end
			end

			
			
		end
		CD.MouseClick:Connect(onMouseClick)

		
	end
	
end

function onMouseClick()
	
	ws.Visible = true
	wait(5)
	
	while true do
		t = 0
		wait(.1)
		t = 1
		wait(.1)
		time1 = time1 + .1
		
		if time1 == complete then
			break
		end
	end

	
	
end
CD.MouseClick:Connect(onMouseClick)

Read the error. You can’t access the settings global in a normal (game) script.

1 Like

So should I use a local script?

No… I just said it is not possible in any scripts in game.

It puts the script in time out because it does not have enough time to process. In every while true do loop you want to add a wait() in it to provide time to process.

There are a lot of red flags in this script.

  • First of all, I would clear up some of the variables at the beginning because you don’t need them.

  • Secondly, you edit a GUI in StarterGui… That will not replicate to any clients already in the game. You need to access the player’s PlayerGui, preferably by using a RemoteEvent.

  • Third, do not loop through the workspace’s descendants like that :fearful: Instead make a table of all of your “Meshes/lamba_Cube.004” and loop through that.

  • You defined 2 global functions with the exact same name… and then changed the function connected to CD.MouseClick

part.Name = BrickColor.new()
Really?

Side notes:

  1. Use RunService instead of a while loop
  2. Avoid using wait(), especially since we have task.wait()
  3. Use less local variables, especially for things you only call once or twice, particularly the constant you defined as “complete”
  4. Get into the habit of using game:GetService() instead of game.ServiceName

Your code could look more similar to this:

local lighting = game:GetService("Lighting")
local rstorage = game:GetService("ReplicatedStorage")
local runservice = game:GetService("RunService")
local CD = script.Parent.ClickDetector
local MeshTable = {} --[[This would be the table with
all of your color-changing lights]]

local function changeColors()
     for i, v in pairs(MeshTable) do
          v.Color = Color3.new(math.random(0,1),math.random(0,1),math.random(0,1))
          --// ^^ Customize to your heart's content ^^
          --// Note that the color changing will be visible to the whole server
          --// To make local, create a RemoteEvent to change the color as well
      end
end 

do --// I used a do block to keep the local variables where I need them
     local connection
     local waitTime = 3 --// The time between each color change
     CD.MouseClick:Connect(function(player)
          if connection then
               connection:Disconnect()
          else
               rstorage.DecoyRemoteEvent:FireClient(player) 
               --// You would want to change any GUI's in the RemoteEvent using PlayerGui
               local t = 0
               connection = runservice.Heartbeat:Connect(function(dt)
                    --// dt is the change in time since the last frame
                    t += dt
                    if t >= waitTime then
                         t -= waitTime
                         changeColors()
                    end
               end)
          end
     end)
end

(I wrote this on the Dev Forum, please be patient with any syntax errors or spelling mistakes!)

Honestly the code you have is extremely cursed. Either this is an elaborate troll or you fell for a terribly made tutorial, but I hope some of this was useful for you!

honestly I would add indentation to the code, would make things easier to see

1 Like

That was my own code if I was being honest. I’m just getting into Roblox LUA.

And there’s really nothing wrong with getting started. You at least took the right steps seeking help and I hope you found some in my post :+1:

part.Name = tostring(BrickColor.new(math.random(0.1,1), math.random(0.1,1), math.random(0.1,1)))

This still wouldn’t work because BrickColor.new() does not take 3 number parameters, it takes the string parameter of the BrickColor name.

Rather what you would want to do is wrap a Color3.new() Inside BrickColor.new() to convert the Color3 to BrickColor3.

part.Name = tostring(BrickColor.new(Color3.new(math.random(0.1,1), math.random(0.1,1), math.random(0.1,1))))
1 Like