Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make sure no BrickColor is repeated when changing a group of brick's BrickColor randomly?

door = script.Parent  
local list = game.ServerStorage.Colors:GetChildren()
local list2 = game.Workspace.Colors4.pads:GetChildren()
local pm = game.ServerStorage.pm.Value
function onChatted(msg, recipient, speaker) 
local num = 0
 

local source = string.lower(speaker.Name) 
msg = string.lower(msg) 

if (msg == ":randomize") then 

repeat 
 
list2[math.random(#list2)].BrickColor = list[math.random(#list)].Value
num = num + 1
print(num)
wait()
until num == 9
end
num = 0
end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 

The script I have works fine but it has 1 problem. I do not want the color to be repeated onto any of the bricks. I tried using variables in a earlier version of this script but that did not work.

Thanks if you decide to help out, thanks if you just read this.

  • UltChowsk

2 answers

✓ Accepted answer
door = script.Parent  
local list = game.ServerStorage.Colors:GetChildren()
local list2 = game.Workspace.Colors4.pads:GetChildren()
local pm = game.ServerStorage.pm.Value
function onChatted(msg, recipient, speaker) 
	local num = 0
	--------------------------
	function colorCheck(tab,val) -- function, so you can do multiple times
		for _,v in pairs (tab) do
			if v == val.Value then
				return false
			else
				return true
			end
		end
	end
	------------------------------
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	-----------------------------------
	local colors = {} -- table for brickcolors
	-----------------------------------
	if (msg == ":randomize") then 
	
	repeat 
	---------------------------------
	function change() -- so you can do multiple times
		local c = list[math.random(#list)]
		if colorCheck(colors,c) == true then
			list2[math.random(#list2)].BrickColor = c.Value
		else
			change()
		end
	end
	change()
	--------------------------------
	num = num + 1
	print(num)
	wait()
	until num == 9
	end
	num = 0
end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 


6 comments

0

I put it in and got a stack overflow.

0

Any idea how to prevent that?

0

It may be becausen you used up all of the brickcolors available.

0

I have 9 seperate colors. Thats what the local variable list is for, I am trying to get it to pick from those 9 to change 9 different bricks without any repeats.

0

Then it should work.

0

I got a stack overflow every time I tried.

I fixed it, I used another script to reset the values and it all works fine now. :D

This discussion is preserved as it was. New answers, comments, and votes are closed.