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
_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:
local SessionData = {} local mana = {} local money = {} function SessionData:UpdateMana(player, manaValue) mana[player] = manaValue end function SessionData:GetMana(player) return mana[player] end function SessionData:UpdateMoney(player, moneyValue) money[player] = moneyValue end function SessionData:GetMoney(player) return money[player] end return SessionData
Example script in workspace part:
local SessionData = require(game.ServerStorage.SessionData) local UpdateManaEvent = Instance.new("RemoteEvent") UpdateManaEvent.Name = "UpdateManaEvent" UpdateManaEvent.Parent = ReplicatedStorage mana = script.Parent mana.Touched:connect(function(hit) local character = hit.Parent if character:FindFirstChild("HumanoidRootPart") then mana:Destroy() --update mana amount local player = game.Players:GetPlayerFromCharacter(character) local manaValue = SessionData:GetMana(player) manaValue = manaValue + value if manaValue > _G.MAX_MANA then manaValue = _G.MAX_MANA end SessionData:UpdateMana(player,manaValue) --we need to update mana counter on player GUI as well UpdateManaEvent:FireClient(player,manaValue) end end)