local DataStore = game:GetService("DataStoreService"):GetDataStore('Moneyx') game.Players.PlayerAdded:Connect(function(player) local S,E = pcall(function() local Got = DataStore:GetAsync(player.UserId) return Got end) if S then if E then print(E) else DataStore:SetAsync(player.UserId,0) -- If No Data Then Set Data to 0 print(player.UserId) end end end)
The problem is I don't have any idea on how I can get other scripts to access the DataStore Value like for example You can have a IntValue in workspace with the loaded data so you can access it but
What I am doing is instead of using IntValues
Using Instances
And I don't think there's another way than to access DataStore everytime I want to change it and SetAsync everytime it's changed
FOR EXAMPLE: PLAYER BUYS A ITEM I WANT TO MINUS HIS MONEY
I CANT JUST GO game.ServerStorage[Player.UserId].Value - 200
I have to do something like game.Script. but I don't see how I can do that
Since this is all done in a script
If you store your data in tables, you can use module scripts to access data across scripts. If you insist on using intvalues/stringvalues/boolvalues, then you can take that data, put it in a table, and save it to datastore.
This way you won't have to save/retrieve from datastore every time you want variables. You just have to update when the player joins and leaves the game. (But I would recommend auto saving every couple minutes as something unexpected can always happen. )
Inside module scripts you can have 'global' functions, variables, etc. (not really global but you can access everything inside a module script when you access it.
Start by putting a ModuleScript inside of your ServerScriptService
The default script will look like this:
local module = {} return module
The code above has module variable. This is is an empty table. The bottom line returns your table. When you acquire to the ModuleScript in another script, you return the module table.
To acquire the module script in another script, type this into a normal script:
myModule = require(game.ServerScriptService.ModuleScript)
if we print out myModule, we will get a table, but the table is currently empty, so there's nothing in it. Let's change that. Go back to the module script and add a variable to the table. Putting the name of the variable 'module' will put our variable 'myVar' inside of module.
local module = {} module.myVar = "Hello World" return module
We can also put functions inside of module: Just remember to put the module variable name in front of it
local module = {} module.myVar = "Hello World" function module.myFunction() print(module.myVar) end return module
Back to the regular script, here's how we'd access the function and variables:
myModule = require(game.ServerScriptService.ModuleScript) myModule.myFunction() --calls myFunction() print(myModule.myVar) --prints myVar
Here's how you'd get the player data using a module script:
local module = {} module.playerData= {} --loads player data from the datastore and puts into modules playerData table function module.loadFromDS(player) local S, data= pcall(function() return DataStore:GetAsync(player.UserId) end) if(S and data and not playerData[player.Name])then playerData[player.Name] = data end end --returns the players data function module.getData(player) return playerData[player.Name] end return module
In the any regular script you can now do this:
myModule = require(game.ServerScriptService.ModuleScript) game.Players.PlayerAdded:Connect(function(player) myModule.loadFromDS(player) --you can get the players data whenever you want, just getting it here as an example. local playersData = myModule.getData(player) end)