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.
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.