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