Hi there! I am infernoProgramer, a developer trying to learn scripting … 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
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?
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
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
WOW these are super helpful! thanks for helping me
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
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