Making a Table for a player

I’m fairly new to tables and was wanting to know how you could make it so that each player has their own table. Sorry if i missed it some where on the wiki.

Important thing to know about Lua: pretty much everything is just a fancy table. Players are tables. SENTs are tables. Weapons are tables. You’re a table. I’m a table.

That being said, you can set a variable on an entity to anything you want as easy as

[lua]LocalPlayer().MyVariable = “Hello, World!”[/lua]

However, bear in mind variables set on players like that are not networked, so if you do that on the client, and on the server you do

[lua]print(pl.MyVariable)[/lua]

it will print nil.

ok thanks for the reply, i’m making a meta table of Player and am having some trouble. here’s my code.
Shared.lua
[lua]

local meta = FindMetaTable(“Player”)
function meta:setRank(rank)
self:SetPData(“Rank”,rank)
end
function meta:getRank()
return self:GetPData(“Rank”)
end
function meta:setFirstUpgrade(option)
self:SetPData(“FirstUpgrade”,option)
end
function meta:setSecondUpgrade(option)
self:SetPData(“SecondUpgrade”,option)
end
function meta:getFirstUpgrade()
return self:SetPData(“FirstUpgrade”)
end
function meta:getSecondUpgrade()
return self:SetPData(“SecondUpgrade”)
end
function meta:getPoints()
return self:GetPData(“Points”)
end
function meta:getSpeedPoints()
return self:GetPData(“SpeedPoints”)
end
function meta:getHealthPoints()
return self:GetPData(“HealthPoints”)
end
function meta:getWeightPoints()
return self:GetPData(“WeightPoints”)
end
function meta:setPoints(points)
self:SetPData(“Points”,points)
end
function meta:setSpeedPoints(points)
self:SetPData(“SpeedPoints”,points)
end
function meta:setHealthPoints(points)
self:SetPData(“HealthPoints”,points)
end
function meta:setWeightPoints(points)
self:SetPData(“WeightPoints”,points)
end
[/lua]
Init.lua
[lua]
function setPlayerStats(ply)
if ply:getPoints() == nil then
ply:setRank(“Noob”)
ply:setPoints(3)
ply:setSpeedPoints(0)
ply:setWeightPoints(0)
ply:setHealthPoints(0)
ply:setFirstUpgrade(“Ammo”)
ply:setSecondUpgrade(“Armor”)
Msg(“First Debug!!!”)
end
Msg(“2nd DeBug!!!”)
end
hook.Add(“PlayerInitialSpawn”,“SetPlayerStats”,setPlayerStats)
[/lua]

So what’s your trouble? Nothing seems blatantly wrong to me.

OH. Issue. All of your meta functions are shared, but you’re not going to be able to set player data from the client, so you’ll get some errors there.

well my problem is that when i try to use something like print(LocalPlayer():getPoints()) it outputs nil.

Also i don’t get the First Debug message in the console.

Mmmmk. But you’re sure you’ve included this shared file or AddCSLua’d it so it would work on the client? This is a gamemode, I presume?

Ya this is a gamemode and yes everything is included and what not.

GetPData will always return something in my experience.

getPoints on client will not work since you’re setting it serverside, network it with ([**Entity.SetNetworkedInt

**](http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt))

You could also use **[Usermessages

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=User_Messages)**

Well i tried using a NWInt but it just returned 0 even though i set it to 3.
I’m thinking that the PlayerInitialSpawn hook isn’t working because im not getting that Msg and nothing is being set.

Why do you need SetPData and GetPData anyway? Those are for permanent data which is stored on the server and stays even if you disconnect and then come back.

I’m using it to make a skill system and a load out system for my gamemode. Kind of like the classes in CoD and skills that effect speed/health/Max weight of all your equipment.

Thanks for all your help you guys i managed to get it working on my dedi so all is good.