Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Value Objects or Global Variables?

Asked by 4 years ago

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 IntValues 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

0
It's actually not advised to use Globals. I can't elaborate on technical reasons, from my understanding they may prove to be inefficient compared to other methods and less secure. However, might I just suggest using multi-dimensional arrays and withholding data that way? M39a9am3R 3210 — 4y
0
Multi-dimensional arrays? Like, a list inside a list? I do that sometimes, but that's for saving player data into a DataStore. Here, I need to save some data which is frequently changed and can be accessed by several Scripts and LocalScripts at any time, preferably without having to call a RemoteFunction every time. RiskoZoSlovenska 378 — 4y
0
I use a module script in the server storage. It is not replicated to clients, so has an extra layer of security. On top of that all data is in the same place and I can easily decide what (if anything) needs to be saved in datastores. Other scripts access that data through functions. I can provide you some code if you interested. sleazel 1287 — 4y
0
What exactly do you mean? So like you require the ModuleScript and it returns some values? I'm a bit confused lol. I need LocalScripts to be able to access all that data, and I think it is pretty secure anyways as even if the client changes the variables, it makes no difference to the server or anything. RiskoZoSlovenska 378 — 4y
0
ServerStorage and ServerScriptStorage are not replicated to clients. That means if you keep data elswhere on server it still cannot be changed by clients other than remote events and functions. Exploiters however can see this data, even if it is some other player data. They have no access whatsoever to server storage though. sleazel 1287 — 4y

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

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)
0
Hmm I think I see what you are doing here. But just some questions: First of all, why do you make a new event from the script? Why not just have it already in ReplicatedStorage? Second, isn't :connect() deprecated in favor of :Connect()? Next, if this Script is the child of mana, and you :Destroy() mana, wouldn't the script get destroyed too? But otherwise, thx, I think I'll do something like this RiskoZoSlovenska 378 — 4y
0
1. I used to do it the other way, but then I ended up with lots of events which I am not sure if I need anymore and I hated it. 2. Yes I should use Connect(). But it is a identical function, it just roblox way to standardise things (all functions should begin with capital letter). 3. Surprisingly not. It just destroys part, descendants and disconnects connections, while the function keeps running. sleazel 1287 — 4y
Ad

Answer this question