I need help with my script

Hi there! I am infernoProgramer, a developer trying to learn scripting :slight_smile:… So I have a problem with my code, I want to add the players in this table and give them a game tag, but it does it for only 1 player and I am trying to insert them into a table but I don’t think it works, can you help me? Here is the code below


local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local ServerStorage = game:GetService(“ServerStorage”)

local Picaxe = ServerStorage.Picaxe

local status = game.ReplicatedStorage.Values.Status

while true do

repeat wait() until game.Players.NumPlayers >= 2 
wait(3)

status.Value = "Round starting"

part 2
note: imagine these added together

local players = game.Players:GetPlayers()
local PlayersInGame = {}

for i, v in pairs(players) do
	table.insert(PlayersInGame,1)
	print(players)
	for i, v in pairs(PlayersInGame) do
		local gameTag = Instance.new("StringValue")
		gameTag.Name = "GameTag"
		gameTag.Parent = v -- I think this stands for the player
		-- then it just keeps repeting and repeting, I try to break out of the for loop, but it won't work, and it does if for only 1 player
	end
end

end

thanks for viewing, and can you teach me what I did wrong?

2 Likes

how table.insert works is you need the table, then the number position, and then the value
so you would want to do

table.insert(PlayersInGame,#PlayersInGame + 1,v.Name)

right now it’s not working because you are telling the code to add something to the “PlayersInGame” in position 1 and your not telling it what should be the value

2 Likes

The way table.insert works is: table.insert(table, item you want to put in table). Right now you are trying to put ‘1’ in the table for each player
If you want to put players inside the table:

table.insert(playersInGame, v) --in this case v is the player

Also, your loops repeat a lot because you are using 2 loops inside of each other.
Loop 1 runs once, then it runs loop 2 which goes through each variable in PlayersInGame,
then loop 1 runs again, and runs loop 2 which again goes through each variable. And so on, until loop 1 finishes.

You should instead use only 1 loop:

    for i, v in pairs(players) do
    table.insert(PlayersInGame, v) --adding the player to your table
    local gameTag = Instance.new("StringValue")
    		gameTag.Name = "GameTag"
    		gameTag.Parent = v --adding tag to the player
    end

Edit: instead of PlayersInGame, an easier way of getting the amount of players in a game is to use #game.Players:GetPlayers(). This returns the number of players in the game

2 Likes

WOW these are super helpful! thanks for helping me :smiley:

and I made the table players in game because players are gonna be in a round so I should’ve replaced players in game with players in round, sorry for that but thank you :smiley:

also I totaly forgot that I put a while true do loop in there and I am still not done with the script so I think thats why it is looping but I will make it all soon

and I will add more to this so you can check every like 2 days if you want

1 Like