This script seems to be slowing my game down a lot

So I made this disco floor script but it keeps on freezing the whole server every time a color changes, I have tried multiple different ways but all turn out the same. Does anyone know the issue, if it is with this script or if it out of my control? Here is the script:

wait(3)
function RandomRainbow(Red, Blue, Yellow, Orange, Green, Purple, White)
	local DiscoMain = game.Workspace.DiscoFloor:GetChildren()
	while wait(1.5) do
		
		for i, v in pairs(DiscoMain) do
			RandomMathe = math.random(1,7)
			if RandomMathe == 1 then
				v.BrickColor = Red
			end
			if RandomMathe == 2 then
				v.BrickColor = Orange
			end
			if RandomMathe == 3 then
				v.BrickColor = Yellow
			end
			if RandomMathe == 4 then
				v.BrickColor = Green
			end
			if RandomMathe == 5 then
				v.BrickColor = Blue
			end
			if RandomMathe == 6 then
				v.BrickColor = Purple
			end
			if RandomMathe == 7 then
				v.BrickColor = White
			end
			
		end

	end


end

RandomRainbow(BrickColor.new("Really red"),BrickColor.new("Bright blue"),BrickColor.new("New Yeller"),BrickColor.new("Neon orange"),BrickColor.new("Bright green"),BrickColor.new("Plum"),BrickColor.new("White"))

2 Likes

How many parts are in the disco floor?

1 Like

There is about 20 - 25 parts, I had a much bigger disco floor before that was not lagging, I forgot to add that.

1 Like

I also only lags when it changes colors.

1 Like

I am noticing the loop. Looks like it is infinite to me, those can sometimes cause lag depending on what they do.

1 Like

I tried making it only run when a certain variable is equal to true, as soon as it turns on it starts lagging again.

1 Like

I don’t see anything wrong with the script but try this one.

local DiscoMain = workspace.DiscoFloor:GetChildren()
local colors = {
    "Really red", "Bright blue", "New Yeller", "Neon orange", "Bright green", "Plum", "White"
}
--why lol
for i, v in next, colors do
    colors[i] = BrickColor.new(v)
end
wait(3)
while wait(1.5) do
    for _, p in next, DiscoMain do
        p.BrickColor = colors[math.random(1, #colors)]
    end
end
1 Like

You may want to set a condition for the loop to stop at.

1 Like

I mean if it becomes a serious lag problem, just do a repeat loop and put a stopping a condition if desired.

1 Like

Why not use BrickColor.Random?

1 Like

BrickColor.Random made it lag more I also only wanted certain colors.

1 Like

You could make use the table and get one random color each time as Esma stated.

1 Like

BrickColor.random() creates really ugly parts in my experience.

1 Like

I will try this out right now.

1 Like

You can make a variable that will get one random color from the table.

1 Like

I put parameters in for that to make it a bit easier as least for me.

1 Like

Try disabling the disco script and see if it still slows down your game. I don’t think its the disco script slowing down your game.

1 Like

This is still creating lag spikes every time it changes color maybe I could make the disco floor into 3 different models each with a different script.

1 Like

It is, I disabled it and my game’s FPS went from like 30 to 45.

1 Like

The OP doesn’t want a condition because the disco floor is a… disco floor. i.e. a floor that changes colors, and I’m sure he wants it to change colors indefinitely.

Also, adding a condition for this loop wouldn’t reduce his lag at all considering he’s lagging every time it changes colors, and not from the fact that it’s running indefinitely.

@henberrysodapop I think your PC simply can’t handle it. There’s no way to reduce the lag that it causes you unless you change the colors one at a time slowly, which I doubt you don’t want. If you do want to try it out, use this code.

local WAIT_TIME = 0.25
local colors = {
	"Really red",
	"Bright blue",
	"New Yeller",
	"Neon orange",
	"Bright green",
	"Plum",
	"White"
}

while wait(1.5) do
	for k, v in pairs(workspace.DiscoFloor:GetChildren()) do
		v.BrickColor = BrickColor.new(colors[math.random(#colors)])
		wait(WAIT_TIME)
		--alternatively you can use wait() for the smallest increment of time
	end
end

Otherwise, there isn’t really a way for this to not lag you. This script doesn’t lag me personally but that’s merely because I have a high-end PC. Editing multiple parts in a short time span will lag you no matter what unless you have a decent PC. And even then, too many parts changing will lag anything.

1 Like