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

How would I make my DataStore Values Global so Scripts can Access it? No Instance

Asked by 5 years ago
Edited 5 years ago
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

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)

1
Im trying to store player data though not a function Overseer_Prince 188 — 5y
0
what does your player data look like royaltoe 5144 — 5y
0
For Now Its a Value that I want to be able to access just like a IntValue in ServerStorage Overseer_Prince 188 — 5y
0
By value to you mean variable in a script or value like intvalue? royaltoe 5144 — 5y
View all comments (9 more)
0
Value in the workspace like int value? royaltoe 5144 — 5y
0
Variable Overseer_Prince 188 — 5y
0
i have a Variable and I want to access it just like a intvalue in server storage Overseer_Prince 188 — 5y
0
Its A Data from GetAsync Value I got Overseer_Prince 188 — 5y
0
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 Overseer_Prince 188 — 5y
0
Yeah. I get what you mean. My answer will allow you to do that. If there's anything you don't understand please ask. royaltoe 5144 — 5y
0
Why tf does this post have 1000+ views greatneil80 2647 — 5y
0
ye BashGuy10 384 — 5y
0
someone committed hold f4 for an hour royaltoe 5144 — 5y
Ad

Answer this question