Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How select a player for give at him a variable?

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.

2 comments

0

you mean tools?

0

Re-change the title to "How to select a particular player to give them an objects."

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:

  1. 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)
  1. 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

0

Woops, I meant to put 2 next to Use Values, not 1

0

Its not very that i want because I want each player to have a different variable

0

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

0

do NOT use this, this runs rapidly and will crash your script

0

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.