My color shuffle game script isn't working

Hi.
I am new to scripting, but I wanted to try to make a roblox game with help of ChatGPT (I somewhat know how to script but Im not good at it). When I finished writing a script, it gives no error messages, but doesnt work. Can you help me find what’s wrong in the script? PS. It’s not a local script but a normal script and it is in ServerScriptStorage

This script for now was supposed to start the game after there are more than 1 player on the server. I posted photo of how the game outside looks. When game starts, the script was supposed to print random color and then all blocks this color would slowly fall and after 4 seconds come back. At the start of game the script was supposed to count the players and when a player die remove him from game. If there was only 1 player left on the map the game would end. I dont know what is wrong with the code, because i am just starting scripting so i need some help with analyzing this script.

local Players = game:GetService("Players")

local function PlayerAdded(plr)
	
end

Players.PlayerAdded:Connect(PlayerAdded)

for _,v in pairs(Players:GetPlayers()) do 
	PlayerAdded(v)
end

local playersOnBoard = {}

local function checkIfPlayerDied()

	for i, player in ipairs(playersOnBoard) do

		if player.Health <= 0 then

			return player  
		end
	end
	return nil  
end

local function initializePlayersOnBoard()
	
	local allPlayers = game.Players:GetPlayers()

	for _, player in ipairs(allPlayers) do
			table.insert(playersOnBoard, player)
		end
	end
local function removePlayerFromBoard(player)

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

	local playerDied = checkIfPlayerDied() 

	if playerDied then
		print(playerDied.Name .. " has died!")
		removePlayerFromBoard(playerDied)
	end


	local blocks = {
		Red = workspace.Colors.Red,
		Blue = workspace.Colors.Blue,
		Green = workspace.Colors.Green,
		Yellow = workspace.Colors.Yellow,
		Purple = workspace.Colors.Purple,
		Brown = workspace.Colors.Brown
	}

	local function generateRandomColor()
		return math.random(1, 6)
	end

	local function displayColorMessage(colorNumber)
		local colors = {"Red", "Blue", "Green", "Yellow", "Purple", "Brown"}
		print(colors[colorNumber] .. " disappears!")

	end

	local function removeBlock(block)
		block.Transparency = 1 
		block.CanCollide = false
	end

	local function blockDisappear()
		local colorNumber = generateRandomColor() 
		local colorName = displayColorMessage(colorNumber) 

		if blocks[colorName] then
			local block = blocks[colorName]
			block.Position = block.Position - Vector3.new(0, 10, 0)  
			wait(1)  

			for transparency = 0, 1, 0.1 do  
				block.Transparency = transparency
				wait(0.1)  
			end

			removeBlock(block)

			wait(4)

			block.Position = block.Position + Vector3.new(0, 10, 0)  
			block.Transparency = 0 
			block.CanCollide = true 
		end
	end
	local players = {}
	local round = 1
	local gameInProgress = false
	local function checkRemainingPlayers()
		local allPlayers = game.Players:GetPlayers()

		if #allPlayers >= 2 then
			print("Game starts!")
			gameInProgress = true
		end
  
		for _, player in ipairs(allPlayers) do
			table.insert(players, player.Name) 
		end
		
		while true do

			if gameInProgress == false then
				print("Waiting for players...")

			else

				local colorNumber = generateRandomColor()


				displayColorMessage(colorNumber)


				blockDisappear()


				if round == 6 then
					print("Game just got harder!")

				end

				if (playersOnBoard == 1) then
					print("Game over, GG!")
					break
				end

				round = round + 1
			end
		end
	end
end

Don’t know if ya meant ServerStorage or ServerScriptService, but it has to be inside ServerScriptService if it isn’t already

exactly, to elaborate, ServerStorage is for storing instances, it doesnt run LocalScript or Script instances.

Yeah it’s in ServerScriptService

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 blocks = {
    Red = workspace.Colors.Red,
    Blue = workspace.Colors.Blue,
    Green = workspace.Colors.Green,
    Yellow = workspace.Colors.Yellow,
    Purple = workspace.Colors.Purple,
    Brown = workspace.Colors.Brown
}

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
        block.Position = block.Position - Vector3.new(0, 10, 0)
        for transparency = 0, 1, 0.1 do
            block.Transparency = transparency
            wait(0.1)
        end
        block.CanCollide = false
        wait(4)
        block.Position = block.Position + Vector3.new(0, 10, 0)
        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(10) -- Adjust time as needed
        end
    else
        print("Not enough players to start the game.")
    end
end

gameLoop()
1 Like

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