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

How do I make a variable for each player?

So I want to make a variable for each player. How do I do that? (I am not requesting a script to do it for me but I honestly have no idea how to do so. I don't need help with anything else, my brain can crunch the rest. Just how do I make a variable for each player?)

I asked other people and here are some thoughts they had.

  1. Folder
  2. Localscript
  3. Just seceding this problem. I won't take number 3 because I am trying to learn, not to just do something and then the next time I run into the same problem again.

1 comment

2 answers

You can set a table for that. And reference each index in the table for a diff variable. At first set each variable equal to nil, and whenever a player is joining insert the player's name into the table and define the variable as the player's name, and when he leaves set it back to nil and remove his value from the table. I hope it was helpful!

1 comment

0

may i have a link to a script with a table I can study?

There are multiple methods for this:

** 1. Use a table **

You can use a table to store values for each player using this script:

local Players = {}
local Data = {}
function Remove(plrName)
	for i, data in pairs(Players) do
		if data == plrName then
			table.remove(Players,i)
			table.remove(Data,i)
			return
		end
	end
end

function Add(plrName)
	table.insert(Players,plrName)
	table.insert(Data,"empty")
end

function Get(plrName)
	for i, data in pairs(Players) do
		if data == plrName then
			return Data[i]
		end
	end
end
function Change(plrName,Change)
	for i, data in pairs(Players) do
		if data == plrName then
			Data[i] = Change
			return
		end
	end
	
end
game.Players.PlayerAdded:Connect(function(plr)
	Add(plr.Name)
end
game.Players.PlayerRemoving:Connect(function(plr)
	Remove(plr.Name)
end

--Your code below! To change the players data, use the change function. To get the players data, use the get function.

local PlayerData = Get(plr.Name) --The get function
print(tostring(PlayerData)

Change(plrName, "Your data here")-- The change function

** 2. Use folders **

You can use folders inside players called "Leaderstats"

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	-- Display an 'IntValue' on leaderboard
	local gold = Instance.new("IntValue")
	gold.Name = "Gold"
	gold.Value = 0
	gold.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)

This method is using those leaderboards you see on top of players.

** 3. Use invisible folders **

Don't want that data being shown to other players via leaderboard? Change this line:

leaderstats.Name = "leaderstats"

to

leaderstats.Name = "stats"

Change the name to whatever you like! It's your choice!

I hope this helps!

Best,

TheLastHabanero

5 comments

0

Hi! Just a quick thing, I am using bool values for this. Does it matter if I change the IntValue to BoolValue?

0

also how do i take those values?

0

Nah if you change it to BoolValue it will make it a Boolean. Make sure to set it to true or false! Sorry for long answer rate. Also, you can change the variable by using plr.leaderstats.VALUE_NAME_HERE

0

Nah if you change it to BoolValue it will make it a Boolean. Make sure to set it to true or false! Sorry for long answer rate. Also, you can change the variable by using plr.leaderstats.VALUE_NAME_HERE

0

Nah if you change it to BoolValue it will make it a Boolean. Make sure to set it to true or false! Sorry for long answer rate. Also, you can change the variable by using plr.leaderstats.VALUE_NAME_HERE

This discussion is preserved as it was. New answers, comments, and votes are closed.