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()
print(myModule.myVar)
Here's how you'd get the player data using a module script:
local module = {}
module.playerData= {}
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
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)
local playersData = myModule.getData(player)
end)
13 comments
Im trying to store player data though not a function
what does your player data look like
For Now Its a Value that I want to be able to access just like a IntValue in ServerStorage
By value to you mean variable in a script or value like intvalue?
Value in the workspace like int value?
Variable
i have a Variable and I want to access it just like a intvalue in server storage
Its A Data from GetAsync Value I got
How I want it is something similar to Basically Player Joins IntValue gets inserted into ServerStorage but instead of using Instances and IntValues I only want to use Values and No IntValues or any type of instances when player joins
Yeah. I get what you mean. My answer will allow you to do that. If there's anything you don't understand please ask.
Why tf does this post have 1000+ views
ye
someone committed hold f4 for an hour