Help with coding with Data

So basically I want to code a money system into my game but I dont want to display the money using leaderstats and I want to add a command that lets you add money but I dont know how to access the money variable from a different script and I want to ask if theres a better or more practical way to do this cuz this is how i did it

image

i see ur new to roblox. the client does not have access to the server storage. however if ur doing it on the server then you can do game.ServerStorage.Data.Currency.Money then you can add by doing game.ServerStorage.Data.Currency.Money.Value += amount

You can use ModuleScripts for managing your data, which is one of the most organized ways, you don’t need to use the ServerStorage at all for managing data, then send RemoteEvents from the client to the server (with a command system of your own) then edit your data accordingly (after sanity checks).

You may also use DataStoreService if you want your data to be persistant across sessions.

Yeah, so the reason it’s not working is because ServerStorage isn’t meant for storing values like money for players. It’s just a storage place for stuff like models or assets (not live game data).

A better way to handle this is to use a ModuleScript that keeps track of each player’s money in a table. Then you can have all your other scripts ask that module how much money a player has, or update it when needed.

You can also use a RemoteEvent if a client (like a button) needs to tell the server to change someone’s money, but always do that change on the server, not the client, to prevent exploiters from faking it.

If you want, I can show you how to set that up step-by-step. It’s a good habit to learn early on, and I’m glad you’re getting immersed in this!

1 Like

That’d be lovely if you would show me

Sure, bro! I’d love to.

  1. Make a ModuleScript (put it in ServerScriptService) - this will hold the money data:
local DataModule = {}
local moneyData = {}

function DataModule.GetMoney(player)
	return moneyData[player] or 0
end

function DataModule.AddMoney(player, amount)
	moneyData[player] = (moneyData[player] or 0) + amount
end

return DataModule
  1. In your other server script, require the module and use it like this:
local Data = require(game.ServerScriptService.DataModule)
Data.AddMoney(player, 50)
print(Data.GetMoney(player))
  1. If you want a button or command on the client to trigger this, you’ll need to use a RemoteEvent. The client fires it, and the server catches it and calls AddMoney() from the module.
1 Like