I need help with my color shuffle game script

Hi, I’m quite new to scripting, but I am making a color shuffle game. The game was supposed to pick random color and then make it disappear from the map.

local RedT = {workspace.Colors.Red, workspace.Colors.Red1, workspace.Colors.Red2, workspace.Colors.Red3}
local BlueT = {workspace.Colors.Blue, workspace.Colors.Blue1, workspace.Colors.Blue2, workspace.Colors.Blue3}
local GreenT = {workspace.Colors.Green, workspace.Colors.Green1, workspace.Colors.Green2, workspace.Colors.Green3}
local YellowT = {workspace.Colors.Yellow, workspace.Colors.Yellow1, workspace.Colors.Yellow2, workspace.Colors.Yellow3, workspace.Colors.Yellow4}
local PurpleT = {workspace.Colors.Purple, workspace.Colors.Purple1, workspace.Colors.Purple2, workspace.Colors.Purple3}
local BrownT = {workspace.Colors.Brown, workspace.Colors.Brown1, workspace.Colors.Brown2, workspace.Colors.Brown3}

local blocks = {
	Red = RedT,
	Blue = BlueT,
	Green = GreenT,
	Yellow = YellowT,
	Purple = PurpleT,
	Brown = BrownT
}

On the map I have many colors but I tried everything and I cant make all parts disappear when they get picked. In the script I made a table with all the parts and then tried to make another table with those colors but no parts disappear. The rest of script works fine, because when I write 1 part instead of a whole table in the second table the part disappears when it gets picked.

Since I can’t see the actual script you are running I don’t know for sure, but I think what’s happening is you are destroying the table instead of the parts. To fix this, instead of

blocks.Red:Destroy()

you would write

for _, v in ipairs(blocks.Red) do
   v:Destroy()
end

Yeah here’s my full code idk how to fix it

local Players = game:GetService("Players")

local playersOnBoard = {}

local function initializePlayersOnBoard()
	for _, player in ipairs(Players:GetPlayers()) do
		table.insert(playersOnBoard, player)
	end
end

local function removePlayer(player)
	for i, p in ipairs(playersOnBoard) do
		if p == player then
			table.remove(playersOnBoard, i)
			break
		end
	end
end

local function onPlayerDied(player)
	removePlayer(player)
	if #playersOnBoard <= 1 then
		print("Game over, GG!")
		game:GetService("Debris"):AddItem(script, 0) -- Self-destruct script
	end
end

local function onCharacterAdded(character)
	character:WaitForChild("Humanoid").Died:Connect(function()
		onPlayerDied(character.Parent)
	end)
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
	table.insert(playersOnBoard, player)
end

Players.PlayerAdded:Connect(onPlayerAdded)

local RedT = {workspace.Colors.Red, workspace.Colors.Red1, workspace.Colors.Red2, workspace.Colors.Red3}
local BlueT = {workspace.Colors.Blue, workspace.Colors.Blue1, workspace.Colors.Blue2, workspace.Colors.Blue3}
local GreenT = {workspace.Colors.Green, workspace.Colors.Green1, workspace.Colors.Green2, workspace.Colors.Green3}
local YellowT = {workspace.Colors.Yellow, workspace.Colors.Yellow1, workspace.Colors.Yellow2, workspace.Colors.Yellow3, workspace.Colors.Yellow4}
local PurpleT = {workspace.Colors.Purple, workspace.Colors.Purple1, workspace.Colors.Purple2, workspace.Colors.Purple3}
local BrownT = {workspace.Colors.Brown, workspace.Colors.Brown1, workspace.Colors.Brown2, workspace.Colors.Brown3}

local blocks = {
	Red = RedT,
	Blue = BlueT,
	Green = GreenT,
	Yellow = YellowT,
	Purple = PurpleT,
	Brown = BrownT
}



local function generateRandomColor()
	local colors = {"Red", "Blue", "Green", "Yellow", "Purple", "Brown"}
	return colors[math.random(1, #colors)]
end

local function blockDisappear()
	local colorName = generateRandomColor()
	print(colorName .. " disappears!")
	local block = blocks[colorName]
	if block then
		for transparency = 0, 1, 0.1 do
			block.Transparency = transparency
			wait(0.1)
		end
		block.CanCollide = false
		wait(4)
		block.Transparency = 0
		block.CanCollide = true
	end
end

local function gameLoop()
	wait(5) -- Wait for players to join
	initializePlayersOnBoard()
	if #playersOnBoard > 1 then
		print("Game starts!")
		while #playersOnBoard > 1 do
			blockDisappear()
			wait(3) -- Adjust time as needed
		end
	else
		print("Not enough players to start the game.")
	end
end

gameLoop()
local function blockDisappear()
	local colorName = generateRandomColor()
	print(colorName .. " disappears!")
	for _, block in ipairs(blocks[colorName] do
		task.spawn(function () 
			for transparency = 0, 1, 0.1 do
				block.Transparency = transparency
				wait(0.1)
			end
			block.CanCollide = false
			wait(4)
			block.Transparency = 0
			block.CanCollide = true
		end)
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.