My code is not working?

Hello I’m making a game the players are not teleporting the blocks are not spawning.
Game:

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local block = repstorage.Block
local board = workspace.Lobby.GameStatus.Board

function createBlock()
	local blockC = block:Clone()
	blockC.Position = Vector3.new(math.random(1, 100),math.random(50),math.random(1, 100))
	blockC.Parent = workspace
end

local playersInGame = {}

local playersAvailable = 0
--Game waiting for 2 players
while true do
	wait()
	for i, v in pairs(players:GetChildren()) do
		if playersAvailable > 2 then
			playersAvailable += 1
		else
			break
		end
	end
end
--Adding player tags
for i, v in pairs(players:GetChildren()) do
	for i, w in pairs(repstorage:GetChildren()) do
		if w.Name == v.Name then
			table.insert(w, playersInGame)
		end
	end
end
--Game
for i, v in pairs(players:GetChildren()) do
	v.Character:MoveTo(Vector3.new(0, 10, 0))
end
while true do
	wait(1)
	createBlock()
	for i, v in pairs(repstorage) do
		if not playersInGame[#playersInGame] < 0 then
			if v:IsA("Boolean") then
				if v.Value == false then
					table.remove(playersInGame)
				end
			end
		else
			playersInGame[#playersInGame].Wins += 1
		end
	end
end

Tags

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"
	local wins = Instance.new("IntValue", folder)
	wins.Name = "Wins"
	local tag = Instance.new("BoolValue", repstorage)
	tag.Name = (player.Name)
	tag.Value = true
end)

players.PlayerRemoving:Connect(function(player)
	for i, v in pairs(repstorage:GetChildren()) do
		if v.Name == player.Name then
			v:Destroy()
		end
	end
end)

Somebody please help.

In your while true do loop, what is createBlock()?

My bad I accidentally didn’t add the entire script here:

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local block = repstorage.Block
local board = workspace.Lobby.GameStatus.Board

function createBlock()
	local blockC = block:Clone()
	blockC.Position = Vector3.new(math.random(1, 100),math.random(50),math.random(1, 100))
	blockC.Parent = workspace
end

local playersInGame = {}

local playersAvailable = 0
--Game waiting for 2 players
while true do
	wait()
	for i, v in pairs(players:GetChildren()) do
		if playersAvailable > 2 then
			playersAvailable += 1
		else
			break
		end
	end
end
--Adding player tags
for i, v in pairs(players:GetChildren()) do
	for i, w in pairs(repstorage:GetChildren()) do
		if w.Name == v.Name then
			table.insert(w, playersInGame)
		end
	end
end
--Game
for i, v in pairs(players:GetChildren()) do
	v.Character:MoveTo(Vector3.new(0, 10, 0))
end
while true do
	wait(1)
	createBlock()
	for i, v in pairs(repstorage) do
		if not playersInGame[#playersInGame] < 0 then
			if v:IsA("Boolean") then
				if v.Value == false then
					table.remove(playersInGame)
				end
			end
		else
			playersInGame[#playersInGame].Wins += 1
		end
	end
end

Is this an addition to what you provided above or is this the entire script?

its the entire script as i aciddentally didnt add the whole script

I edited 2 things in your script but i don’t think it fixed it

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local block = repstorage.Block
local board = workspace.Lobby.GameStatus.Board

function createBlock()
	local blockC = block:Clone()
	blockC.Position = Vector3.new(math.random(1, 100),math.random(50),math.random(1, 100))
	blockC.Parent = workspace
end

local playersInGame = {}

local playersAvailable = 0
--Game waiting for 2 players
while true do
	wait()
	for i, v in pairs(players:GetChildren()) do
		if playersAvailable > 2 then
			playersAvailable += 1
		else
			break
		end
	end
end
--Adding player tags
for i, v in pairs(players:GetChildren()) do
	for i, w in pairs(repstorage:GetChildren()) do
		if w.Name == v.Name then
			table.insert(w, playersInGame)
		end
	end
end
--Game
for i, v in pairs(players:GetChildren()) do
	if v.Character then
		v.Character:MoveTo(Vector3.new(0, 10, 0))
	end
end
while true do
	wait(1)
	createBlock()
	for i, v in pairs(repstorage) do
		if playersInGame[#playersInGame] > 0 then
			if v:IsA("Boolean") then
				if v.Value == false then
					table.remove(playersInGame)
				end
			end
		else
			playersInGame[#playersInGame].Wins += 1
		end
	end
end

There are several problems with your script.

  1. Infinite loop: The first while loop in your code is an infinite loop as there is no exit condition. This loop will run indefinitely and cause the script to crash.
  2. Syntax errors: In the second for loop where you are trying to add player tags, you are trying to insert playersInGame into the table w. This is incorrect syntax. You need to insert the player object into the playersInGame table.
  3. Missing reference to playersInGame: In the for loop where you are iterating over repstorage, you are trying to check if playersInGame[#playersInGame] < 0. However, you have not initialized playersInGame anywhere in your code, so this will throw an error.

Try this script instead:

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local block = repstorage.Block
local board = workspace.Lobby.GameStatus.Board

function createBlock()
    local blockC = block:Clone()
    blockC.Position = Vector3.new(math.random(1, 100),math.random(50),math.random(1, 100))
    blockC.Parent = workspace
end

local playersInGame = {}

local playersAvailable = 0

-- Wait for 2 players
while playersAvailable < 2 do
    wait()
    playersAvailable = 0
    for i, v in pairs(players:GetChildren()) do
        playersAvailable += 1
    end
end

-- Add player tags
for i, v in pairs(players:GetChildren()) do
    for i, w in pairs(repstorage:GetChildren()) do
        if w.Name == v.Name then
            table.insert(playersInGame, v)
        end
    end
end

-- Game
for i, v in pairs(players:GetChildren()) do
    v.Character:MoveTo(Vector3.new(0, 10, 0))
end

while true do
    wait(1)
    createBlock()
    for i, v in pairs(repstorage:GetChildren()) do
        if playersInGame[#playersInGame] ~= nil then
            if v:IsA("BoolValue") then
                if v.Value == false then
                    table.remove(playersInGame)
                end
            else
                playersInGame[#playersInGame].Wins += 1
            end
        end
    end
end
1 Like

It seems that you are not waiting for the characters to exist and that may be an error

1 Like

Yeah he is also not waiting for v.Character

1 Like

I tried changing those things but it still hasn’t been fixed I also tried your script but that didn’t work either.

Try this one.

local repstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local block = repstorage.Block
local board = workspace.Lobby.GameStatus.Board

function createBlock()
	local blockC = block:Clone()
	blockC.Position = Vector3.new(math.random(1, 100),math.random(50),math.random(1, 100))
	blockC.Parent = workspace
end

local playersInGame = {}

local playersAvailable = 0
--Game waiting for 2 players
while true do
	wait()
	for i, v in pairs(players:GetChildren()) do
		if playersAvailable < 2 then
			playersAvailable = playersAvailable + 1
		else
			break
		end
	end
end
--Adding player tags
for i, v in pairs(players:GetChildren()) do
	for i, w in pairs(repstorage:GetChildren()) do
		if w.Name == v.Name then
			table.insert(playersInGame, v.Name)
		end
	end
end
--Game
for i, v in pairs(players:GetChildren()) do
	v.Character:MoveTo(Vector3.new(0, 10, 0))
end
while true do
	wait(1)
	createBlock()
	for i, v in pairs(repstorage:GetChildren()) do
		if playersInGame[#playersInGame] > 0 and v:IsA("Boolean") then
			if v.Value == false then
				table.remove(playersInGame)
			end
		else
			playersInGame[#playersInGame].Wins = playersInGame[#playersInGame].Wins + 1
		end
	end
end

That still didn’t work, also I tried changing the math.random(50) to just 50 and it still didn’t work.

Why you guys looping for all players instead of getting the total amount of players using #Players:GetPlayers()?

Are there any errors on output?

No and as for your last question, I just forgot, but wouldn’t it work the same?

Well it is more readable and I think it is a faster method, is this issue solved or do you still need help?

I rechecked this code and here’s what I found:

  1. In the following loop, you are not actually counting the number of players available:
for i, v in pairs(players:GetChildren()) do
    if playersAvailable > 2 then
        playersAvailable += 1
    else
        break
    end
end

You are only checking whether playersAvailable is greater than 2 and incrementing it if it is. Instead, you might want to increment it for every player you find until you reach 2:

for i, v in pairs(players:GetChildren()) do
    if playersAvailable >= 2 then
        break
    else
        playersAvailable += 1
    end
end
  1. In the following loop, you are trying to add the playersInGame table to the w object, but w is a child of repstorage, which is not a valid data type for a Lua table:
for i, v in pairs(players:GetChildren()) do
    for i, w in pairs(repstorage:GetChildren()) do
        if w.Name == v.Name then
            table.insert(w, playersInGame)
        end
    end
end

You might want to consider creating a separate table for each player in playersInGame, and adding those to a separate table in repstorage:

local playerTables = {}

for i, v in pairs(players:GetChildren()) do
    local playerTable = {}
    table.insert(playerTables, playerTable)
    for j, w in pairs(repstorage:GetChildren()) do
        if w.Name == v.Name then
            w:SetAttribute("PlayersInGame", playerTable)
        end
    end
end
  1. In the following loop, you are iterating over repstorage, which is not a table, so pairs() will not work as expected:
for i, v in pairs(repstorage) do
    if not playersInGame[#playersInGame] < 0 then
        if v:IsA("Boolean") then
            if v.Value == false then
                table.remove(playersInGame)
            end
        else
            playersInGame[#playersInGame].Wins += 1
        end
    end
end

You might want to consider iterating over the playerTables table instead, and accessing the Boolean values using GetAttribute():

for i, v in pairs(playerTables) do
    if not playersInGame[#playersInGame] < 0 then
        local booleanValue = v.BooleanValue:GetAttribute("Value")
        if booleanValue == false then
            table.remove(playersInGame)
        else
            playersInGame[#playersInGame].Wins += 1
        end
    end
end

I hope this helps you identify and fix the issues in your code! Let me know if you get any errors or if it didn’t work.

question where is this script and what tyoe if scrpt is it ?


local RS = game:GetService("ReplicatedStorage")
local Players = game.Players


local Block = RS:FindFirstChild("Block")

local PlayerArray = {}


function DupeBlock()
	
	
local ClonedBlock = Block:Clone()
	
ClonedBlock.Position = Vector3.new(math.random(),math.random(),math.random())
ClonedBlock.Parent = workspace	
	
end

function GetPlayerList() -- Gets player list function  
	
	
	for i,Players in pairs(Players:GetPlayers()) do -- gets list of players 
		
		
		if i > 2 then -- checks if players is more then 2 players 
			
			
			for i, Objects in pairs(RS:GetChildren()) do -- gets objects from Replicated Storage?
				
				if Objects.Name == Players.Name then -- checks if object name is equal to the players name ?
					
					table.insert(Objects.Name, PlayerArray) -- Puts the Rs object name  in a Table ?
					
				end
				
			end
			-- Requirement Reached
			
			Players:FindFirstChild("Character"):FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(0,10,0) -- Changes everyones position to this cframe
			
		else
			
			
			--Below Requirement
			
		end
		
		
	end	
	
	
end

i would help you more but i don’t really understand what you are trying to achieve since you havent clearly stated the issue or the end result