[SOLVED] Need Help with values in player again

I am creating a prison game and need help with organization in player and more. By the time the game is done, the player would have over 30+ values inside it to create checks and stuff, but when I try to update the game again other scripters including myself would be EXTREMELY confused. I have asked another question similar to this here: https://devforum.roblox.com/t/value-organization-help/339281/4

but it seems that I have to make the dictionary global, in order to store all the player values which I heard and believe is bad practice and unorganized. How would I try to organize everything in the player(mostly values like isInnocent, officerWarnings, etc)? Should I group the values and try to organize them that way(folders don’t work because they would insert a leaderboard)? If you have game creating experience, please tell me how you organize everything.

1 Like

Actually, you should use a map of all these values and put the Player’s name as the key and another map to put the officerWarnings and all those values in them.

I have to make 2 maps?!?!? Also, won’t I have to make the values global in order to access them throughout every script?

local module = {}
local playerMap = {}

function module.setKey(keyName, value)
	playerMap[keyName] = value
end

function module.getKey(key)
	return playerMap[key]
end

function module.getKeys() -- iterate through this one with a for loop
	return playerMap;
end

return module;

You can add keys and set them like this:

module.setKey("Player1", {
	officerWarnings = 0
})

Just some food for thought.

1 Like

So I should just use modules in order to access those values?

You won’t have to make them global, and you can make new values and that kind of thing.

Yes you should use modulescripts, they cache the data so you don’t have to use something insecure like a global table, and you can put the module in the serverstorage and use remotefunctions to get them from the client, so exploiters can’t edit the values.

Just a note, exploiters shouldn’t be able to edit ValueObjects in their player anyways (well they can edit it but it just wont replicate to the server)

I would have to always use remote functions in order to access them?

That is true, however it just is a lot more messy and is not in use in more modern games.

tbh I prefer using value objects in the player for small things like Gold, exp, stamina so they don’t have to invoke an RF to the server and I can use it in gui easily

It’s the best way to do that, here’s an example:

-- Server
RemoteFunction.OnServerInvoke = function(Player, Key)
	for _, value in pairs(module.getKey(player.Name)) do
		if value == Key then
			return value
		end
	end
end
-- Client
RemoteFunction:InvokeServer(Player, "officerWarnings")
1 Like

Thanks. Do I have to create my own changed function in order to detect if a value has changed?

If you want to know when something has changed, fire a bindableevent, it works like a remoteevent except it’s e
replicated across two server/client scripts.

Thanks, I will look into all of this.

Another question, is all of this performance-taking? Or does it not make a huge difference in performance compared to just normal values?

Nope, you won’t have to use too much performance, it MIGHT be a little bit more than values, but doing this is more common and you should familiarize yourself with ModuleScripts.

2 Likes

@mobyboyy Thanks a lot for your patience w/ @SupaOverpowered … so I could learn now one year later a lot!