Garry's Mod

Garry's Mod

Florian's Script - Character System
 Denne tråd er blevet fastgjort, så den er sikkert vigtig
Florian #  [udvikler] 6. mar. 2020 kl. 12:41
[EN] How do I add custom information to characters ?
In addition to the data normally saved by the script, you have the possibility to add data for an X or Y reason.

Small notice: these scripts will have to run exclusively on the server side, so you should include it in the "lua/autorun/server" folder or in a folder where you can be sure it will be executed on the right side.

First, you'll have to declare it... To do this, we'll use the "PlayerInitialSpawn" hook which is triggered the first time the player spawn on the server, the code will look like this :

hook.Add("PlayerInitialSpawn", "AddCharacterInformation", function(ply) FScript.AddCharacterInformation("Stamina", function() return ply:getDarkRPVar("Stamina") end) end)

The example above shows that we will record a DarkRP variable about the player's stamina. From now on, each time the character is saved, the script will take this value and will be saved like all the others in the same file.

Once the declaration is finished and thus the configuration of the save, we will proceed to the step of loading this value. Each time a character is loaded, the "FScript.OnCharacterLoaded" hook will be triggered, this is when we will assign this value to the character.

hook.Add("FScript.OnCharacterLoaded", "GetCharacterInformation", function(ply, data) local Stamina = data["Stamina"] if Stamina then -- If the variable does exist. ply:setDarkRPVar("Stamina", Stamina) end end)

So the server will assign this value if it exists in the loaded data. But there is one last step: the reset. Indeed, each time a character changes, the player resets to default values to allow new values to be received.
To do this, we will use the "FScript.OnResetPlayer" hook as below.

hook.Add("FScript.OnResetPlayer", "ResetCharacterInformation", function(ply) ply:setDarkRPVar("Stamina", 100) end)

The code below therefore resets the DarkRP variable between each character change.

And... that's it!

You can perform this operation as many times as you want without abuse of course... ☺
Sidst redigeret af Florian #; 7. mar. 2020 kl. 7:33