DataStore2 - Script Problem and Question

I am using DataStore2 but I think something went wrong with my script as the gui is not showing the text its supposed to show after receiving the information. I also want to ask if DataStore2 can be used across different places in the same game. I have tried looking into some tutorials but I could not find the problem with my script becuase most of the time I am very blur.

--server script
local rs = game:GetService("ReplicatedStorage")

local DataStore2 = require(1936396537)

local defaultTokens = 5

game.Players.PlayerAdded:Connect(function(player)
	local TokenData = DataStore2("Tokens",player)
	
	local function UpdateClientToken(Value)
		rs.UpdateTokens:FireClient(player, Value)
	end
	
	UpdateClientToken(TokenData:Get(defaultTokens))
	
	TokenData:OnUpdate(UpdateClientToken)
end)
--local script in gui
game.ReplicatedStorage.UpdateClientTokens.OnClientEvent:Connect(function(value)
	script.Parent.Text = value
end)

So which part of my script has an error, I know I can use print to find it but it is not very effective for me in this situation.

2 Likes

In your script change it to the following,

--- Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(1936396537)
local DefaultTokens = 5

game.Players.PlayerAdded:Connect(function(player)
    local Data = DataStore2("Tokens", player)
    local Tokens = Instance.new("IntValue", player) -- create an int value to keep track of tokens (can be something else if needed)
    
    local function UpdateTokens(UpdatedValue)
        Tokens.Value = Data:Get(UpdatedValue)
        ReplicatedStorage:WaitForChild("UpdateClientTokens"):FireClient(player, Tokens.Value)
    end

    UpdateTokens(DefaultTokens)
    Data:OnUpdate(UpdateTokens)
end)

--- Local Script
local Event = game:GetService("ReplicatedStorage"):WaitForChild("UpdateClientTokens")
Event.OnClientEvent:Connect(function(value)
    script.Parent.Text = value
end)

A few issues I noticed in your script(s) were,

  1. The “UpdateTokens:FireClient” was different than the local script’s “UpdateClientTokens” I’m not sure if they’re two seperate events, defined differently, or just named wrong.
  2. You didn’t do Data:Get() in your UpdateTokens function in the server script.

Correct me if I’m wrong with my code samples but this should hopefully fix your issue. More documentation on DataStore2 can be found here

1 Like

I was going to reply with the same answer as @Notrealac0unt. You did not go through the datastore 2 api properly. For more information watch alvinblox’s video on it: https://www.youtube.com/watch?v=hBfMfB0BwGA&t=916s

1 Like

The gui is still showing the loading screen and has not shown any value. By the way, can DataStore2 be used across multiple places in a game?

1 Like

DataStore2 should work across all games if done correctly

2 Likes

Thanks for the information —

1 Like

if the value is the tokens then just change “script.Parent.Text = value” to

script.Parent.Text = game.Players.LocalPlayer.Tokens.Value

I recommend learning advanced lua

But the problem is there is no child called Tokens under the player. By the way, it showed 5 once when playing in studios but it did not appear again the other times I tried it

1 Like

After “local tokens = instance.new…” do:

Tokens.Parent = player
Tokens.Name = "Tokens"

then the method in the previous reply should work.

What was the problem…? This will help me learn :grin:

I managed to send the Data over by changing from

local rs = game:GetService("ReplicatedStorage")
--fire client script bla bla

to

--fire client script without creating a local for replicated storage 
game.ReplicatedStorage.--Your Remote Event

But I realised another problem arose, I could not update the value in studios and sometimes it would not show the value in studios during some attempts