Score storage

Okay, so I’m just chillin’ out maxin’ out relaxin’ all cool and shootin’ some b-ball outside the school, and I wondered if there was a way to store a score on the client and server.

What I mean is in my certain gamemode, when a player collects a certain entity I want it to add one to a value that can be utilized in the HUD and remembered on the client’s machine next time he loads the gamemode.

Is this possible? Is this similar to the money system in RP gamemodes?

[editline]30th January 2011[/editline]

I have the entity all set up, and I am capable of running a script when a player collides.

Player:SetPData

Thanks, just a question, where is this data saved?

[editline]30th January 2011[/editline]

I also realize this should be in the questions section, just a mistake. But would the data be saved in a text file on the server or client’s machine or something?

SERVER:

For an entity:
[lua]
function ENT:Touch(hitEntity)
if ( IsValid(hitEntity) and hitEntity:IsPlayer() ) then
local newScore = (hitEntity:GetPData(“score”) or 0) + 1;
hitEntity:SetPData(“score”, newScore);
hitEntity:SetNWInt(“score”, newScore);
end;
end;
[/lua]

**[G.ValidEntity

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

**[Entity.IsPlayer

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

**[Player.SetPData

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

**[Entity.SetNetworkedInt

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

CLIENT:

For a gamemode:
[lua]
function GM:HUDPaint()
local iScore = LocalPlayer():GetNWInt(“score”);
draw.WordBox( 4, 32, 32, "Score: "…iScore, “Default”, Color(0, 0, 0, 100), Color(255, 255, 255, 255) );
end;
[/lua]

For an addon:
[lua]
hook.Add(“HUDPaint”, “ScoreDraw”, function()
local iScore = LocalPlayer():GetNWInt(“score”);
draw.WordBox( 4, 32, 32, "Score: "…iScore, “Default”, Color(0, 0, 0, 100), Color(255, 255, 255, 255) );
end);
[/lua]

**[Entity.GetNetworkedInt

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

**[Hook.Add

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

**[Gamemode.HUDPaint

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

**[ENT.Touch

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

**[Draw.WordBox

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

It is stored in the server’s database garrysmod/garrysmod/sv.db .

-snip, wrong thread-

Thanks a bunch, somescripter, really appreciate it.

[editline]30th January 2011[/editline]

It works! Melon collecting system complete! Now onto the HUD