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 11 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:

01local DataStoreService = game:GetService("DataStoreService")
02game.Players.PlayerAdded:connect(
03function(Player)
04    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
05end
06)
07local StatisticsStorage = DataStoreService:GetDataStore("Statistics", "ServerData")
08local CashValue = StatisticsStorage:GetAsync("Cash")
09print(CashValue)
10StatisticsStorage:SetAsync("Cash", 500)

And the script in the TextBox looks like this:

01local display = script.Parent
02local text = display.Text
03 
04local DataStoreService = game:GetService("DataStoreService")
05game.Players.PlayerAdded:connect(
06function(Player)
07    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
08end
09)
10local StatisticsStorage = DataStoreService:GetDataStore("Statistics", "ServerData")
11StatisticsStorage:OnUpdate("Cash",
12function(NewValue)
13    text = "Cash: "..NewValue..
14    print("Cash value has changed! The new value is: "..NewValue)
15end
16)

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
11 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!

01local DataStoreService = game:GetService("DataStoreService")
02game.Players.PlayerAdded:connect(
03function(Player)
04    local PlayerStore = DataStoreService:GetDataStore(tostring(Player.userId), "PlayerData")
05    local Cash = PlayerStore:GetAsync("Cash")
06    if not Cash then -- No value initialized
07        Cash = 500
08        PlayerStore:SetAsync("Cash", Cash)
09    end
10    local Value = game.ReplicatedStorage:FindFirstChild(tostring(Player.userId) ,, "Cash") -- a value tracker
11    -- no value?
12    if not Value then
13        Value = Instance.new("NumberValue", game.ReplicatedStorage)
14        Value.Name = tostring(Player.userId) .. "Cash"
15    end
View all 21 lines...

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

1local Name = game.Workspace.Name
2Name = "Hi"
3print(game.Workspace.Name) --> Workspace, not Hi, like you intended
4-- How to fix??
5game.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.

01local player  = game.Players.LocalPlayer
02repeat wait() until game.ReplicatedStorage:FindFirstChild(tostring(player.userId).."Cash")
03local value = game.ReplicatedStorage[tostring(player.userId).."Cash")]
04value.Changed:connect(function()
05    local new = value.Value
06    script.Parent.Text = "Cash: "..new
07end)
08while wait(1) do -- Update the cash
09    value.Value = value.Value + 1
10end

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 — 11y
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 — 11y
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 — 11y
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 — 11y
View all comments (4 more)
0
Btw I noticed an error in your script, you put ,, instead of ..! Just pointing that out. :) AwsomeSpongebob 350 — 11y
0
Where exactly? I can't seem to find it... (line number?) jobro13 980 — 11y
0
10. AwsomeSpongebob 350 — 11y
0
A .. was needed instead of a ,, . ,, I dont think can be used in lua. AwsomeSpongebob 350 — 11y
Ad

Answer this question