In my game, I want to store per-player data, for example, how many kills they have, how many items they collected, etc. (Not permanently, just so scripts can work with it.) The way I do this now is that I have a Script
detect when a new player joins, and then it creates a Folder
under the Player
objects and fills it with IntValue
s and so on. This works pretty well, and is easy to debug because you can see the data without having to print it, but I was wondering if storing data in global (_G.
type things) dictionaries would be better. For example, instead of giving each player an IntValue
called “kills” and then doing things with that, I would have a global dictionary (_G.playerKills = {}
or something like that), and then I would store data using the player’s name. So if I wanted to store 10 kills for player named “TestPlayer”, I would do
1 | _G.playerKills [ player.Name ] = _G.playerKills [ player.Name ] + 10 --'player' is the "TestPlayer" player object |
So, what is more efficient? Which method is neater? Which one would you use? Anything helps :D thx
Module Script in ServerStorage:
01 | local SessionData = { } |
02 | local mana = { } |
03 | local money = { } |
04 |
05 | function SessionData:UpdateMana(player, manaValue) |
06 | mana [ player ] = manaValue |
07 | end |
08 |
09 | function SessionData:GetMana(player) |
10 | return mana [ player ] |
11 | end |
12 |
13 | function SessionData:UpdateMoney(player, moneyValue) |
14 | money [ player ] = moneyValue |
15 | end |
16 |
17 | function SessionData:GetMoney(player) |
18 | return money [ player ] |
19 | end |
20 |
21 | return SessionData |
Example script in workspace part:
01 | local SessionData = require(game.ServerStorage.SessionData) |
02 | local UpdateManaEvent = Instance.new( "RemoteEvent" ) |
03 | UpdateManaEvent.Name = "UpdateManaEvent" |
04 | UpdateManaEvent.Parent = ReplicatedStorage |
05 | mana = script.Parent |
06 | mana.Touched:connect( function (hit) |
07 | local character = hit.Parent |
08 | |
09 | if character:FindFirstChild( "HumanoidRootPart" ) then |
10 | mana:Destroy() |
11 |
12 | --update mana amount |
13 | local player = game.Players:GetPlayerFromCharacter(character) |
14 | local manaValue = SessionData:GetMana(player) |
15 |
16 | manaValue = manaValue + value |
17 | if manaValue > _G.MAX_MANA then |
18 | manaValue = _G.MAX_MANA |
19 | end |
20 |
21 | SessionData:UpdateMana(player,manaValue) |
22 | |
23 | --we need to update mana counter on player GUI as well |
24 | UpdateManaEvent:FireClient(player,manaValue) |
25 |
26 | end |
27 | |
28 | end ) |