What I want to do is give each player a variable to give them different objects to each of them but I want to know how to choose the player number 1, number 2, number 3 etc.
How select a player for give at him a variable?
2 answers
You don't know how to iterate all players? It's fairly easy.
You need to use the GetPlayers() function of the Players service, then use it in a for loop, like so:
for i,plr in pairs(game.Players:GetPlayers()) do
--do stuff
--plr is the player
end
If you want to store a variable per player, you have two options:
- Use tables
Make a dictionairy table of tables with data, in which the keys are the player names, add values to it when a player joins with PlayerAdded event
local data = {}
game.Players.PlayerAdded:Connect(function(plr)
--load data if necessary
data[plr.Name] = {Cash = 0}
end)
game.Players.PlayerRemoving:Connect(function(plr)
--save data if necessary
data[plr.Name] = nil
end)
- Use Values
You can use values instances such as StringValue and parent them to the player when they join
game.Players.PlayerAdded:Connect(function(plr)
local cash = Instance.new("NumberValue", plr)
cash.Name = "Cash"
cash.Value = 0
end)
3 comments
Woops, I meant to put 2 next to Use Values, not 1
Its not very that i want because I want each player to have a different variable
That will make each player have a different variable
while true do
local players = game.Players:GetChildren()
local radplr = math.random(1, #players)
print(radplr .. " chosen")
end
2 comments
do NOT use this, this runs rapidly and will crash your script
You would use a generic for loop for this. While true do loop is not the correct approach for this problem.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
you mean tools?
Re-change the title to "How to select a particular player to give them an objects."