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

The Data Store wont work. I think I may have done something wrong, can anyone help?

Asked by 10 years ago

Okay, so I have two scripts. One basic script in the workplace that makes a "Cash" value for players that is stored in the data store, and another basic script in a TextBox that is supposed to display a players cash value for them. The work space code looks like this:

local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(
function(Player)
    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
end
)
local StatisticsStorage = DataStoreService:GetDataStore("Statistics", "ServerData")
local CashValue = StatisticsStorage:GetAsync("Cash")
print(CashValue)
StatisticsStorage:SetAsync("Cash", 500)

And the script in the TextBox looks like this:

local display = script.Parent
local text = display.Text

local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(
function(Player)
    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
end
)
local StatisticsStorage = DataStoreService:GetDataStore("Statistics", "ServerData")
StatisticsStorage:OnUpdate("Cash", 
function(NewValue) 
    text = "Cash: "..NewValue..
    print("Cash value has changed! The new value is: "..NewValue)
end
)

Am I doing something wrong? Assistance is greatly appreciated. Thanks!

Also, the TextBox is in a GUI in the Starter GUI. The text doesnt display at all.

1 answer

Log in to vote
2
Answered by
jobro13 980 Moderation Voter
10 years ago

The second script crashes because my guess is that it is a LocalScript. You cannot use LocalScripts for this job - it will error and stop your script.

So why doesn't it work? It's a logic error here. You are setting cash to StatisticsStorage to the key Cash - this is ALWAYS the same key and it will only be run once on one server. This is not what you want: you want to use it per-player!

local DataStoreService = game:GetService("DataStoreService")
game.Players.PlayerAdded:connect(
function(Player)
    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
    local Cash = PlayerStore:GetAsync("Cash")
    if not Cash then -- No value initialized
        Cash = 500
        PlayerStore:SetAsync("Cash", Cash)
    end
    local Value = game.ReplicatedStorage:FindFirstChild(tostring(Player.userId) ,, "Cash") -- a value tracker
    -- no value?
    if not Value then
        Value = Instance.new("NumberValue", game.ReplicatedStorage)
        Value.Name = tostring(Player.userId) .. "Cash"
    end
    Value.Changed:connect(function()
        local NewValue = Value.Value -- The value of this value denoting the cash
        PlayerStore:SetAsync("Cash", Cash)
    end)
end
)

I normally would suggest using UpdateAsync, but in cases where only one server usually writes (for instance for per-player data, like here) I'd suggest just using SetAsync.

Also, I'd like to note the difference between instances and other types. On line 2 of your local script you store a variable called "text". This text is the actual value of the text in the TextBox, NOT the property itself! This is a huge difference! Setting text to something new won't change anything to the TextBox itself as you just set the variable text to something new.

For instance, try

local Name = game.Workspace.Name
Name = "Hi"
print(game.Workspace.Name) --> Workspace, not Hi, like you intended
-- How to fix??
game.Workspace.Name = "Hi"

As general rule: use a . when you try to edit properties, and otherwise don't use that unless you are working with tables. game.Workspace denotes the roblox instance, game.Workspace.Name denotes the property, not the "shortcut" to the property. Keep this in mind.

local player  = game.Players.LocalPlayer
repeat wait() until game.ReplicatedStorage:FindFirstChild(tostring(player.userId).."Cash")
local value = game.ReplicatedStorage[tostring(player.userId).."Cash")]
value.Changed:connect(function()
    local new = value.Value
    script.Parent.Text = "Cash: "..new
end)
while wait(1) do -- Update the cash
    value.Value = value.Value + 1
end

Try to understand these scripts and comment if you don't understand something.

0
Thanks for the help! This really explains a lot. However just one question, What is replicated storage? AwsomeSpongebob 350 — 10y
0
ReplicatedStorage is basically what most users used Lighting for before. The ReplicatedStorage is where we put objects in which we want to replicate to clients, for example their cash values. You could also have used the Player instance itself for this or Lighting, but sticking with ReplicatedStorage is "cleaner". Yet, I must admit, putting it inside the Player in this case would be easier as it w jobro13 980 — 10y
0
Ohh ok. So basically its like storing a model in the lighting to clone, but easier right? I gotcha. Coolio, but one more thing- I am not using any local scripts, should I be? AwsomeSpongebob 350 — 10y
0
I suggest using a local script for the second. If you use server scripts for GUIs, you will notice lag - which you don't want. This script is compatible to be local. And yeah, ReplicatedStorage is just like Lighting, but now it's real purpose is to store things - Lighting was not really intended for this, but everyone used it. ReplicatedStorage is the "official" lighting now. jobro13 980 — 10y
View all comments (4 more)
0
Btw I noticed an error in your script, you put ,, instead of ..! Just pointing that out. :) AwsomeSpongebob 350 — 10y
0
Where exactly? I can't seem to find it... (line number?) jobro13 980 — 10y
0
10. AwsomeSpongebob 350 — 10y
0
A .. was needed instead of a ,, . ,, I dont think can be used in lua. AwsomeSpongebob 350 — 10y
Ad

Answer this question