Database2 - getting data from localscript

Good Day,

I am trying to understand Database2, I can’t understand how to get a value from a script or localscript. I am using a RemoteFunction to send a request to the server but cant figure out how to call for the data. Below is my basic setup of my leaderstats with database2.

A basic example would be great.

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Remote = ReplicatedStorage:WaitForChild(“RF-GetCoins2”)

local defaultValue = 0

game.Players.PlayerAdded:Connect(function(plr)

local coinsDataStore = DataStore2("coins",plr)

local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"

local coins = Instance.new("IntValue",leaderstats)
coins.Name = "coins"

local function coinsUpdate(UpdatedValue)

	coins.Value = coinsDataStore:Get(UpdatedValue)
end

coinsUpdate(defaultValue)
coinsDataStore:OnUpdate(coinsUpdate)

end)

So basically you want to make something like:
player.leaderstats.Coins.Value + 1?

If yes then make something to invoke server
for example Button in StarterGui:

Local script inside Button:

local Button = script.Parent
Button.MouseButton1Click:Connect(function()
game.ReplicatedStorage:WaitForChild("YourRemoteFunctionName):InvokeServer()
end)

Script inside ServerScriptService:

local function addCoins(player)
if player.Name == player then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
return true
end
return false
end)

game.ReplicatedStorage:WaitForChild(YourRemoteFunctionName).OnServerInvoke = addCoins

I think you mean DataStore2 lol

Hi,

Thanks for the help, when I run the script I get the error below on the last line of the server script.

OnServerInvoke is not a valid member of RemoteEvent “ReplicatedStorage.RemoteEventTest”


– ReplicatedStorage

RemoteEventTest (RemoteEvent)


– ServerScript (serverscriptservice)

local function addCoins2(player)
if player.Name == player then
player.leaderstats.coins.Value = player.leaderstats.coins.Value + 1
return true
end
return false
end
game.ReplicatedStorage:WaitForChild(“RemoteEventTest”).OnServerInvoke = addCoins2


– LocalScript (ScreenGUI)

local button = script.Parent.Parent.DB2
local player = game.Players.LocalPlayer

button.MouseButton1Click:Connect(function()
game.ReplicatedStorage:WaitForChild(“RemoteEventTest”):InvokeServer()
end)

You need RemoteFunction in this case not RemoteEvent.

Thanks I have got it working thanks.

You can use RemoteEvents too but I am comfortable with using RemoteFunctions :).

No problem, I am glad that I helped :+1:.

However I am not very similiar to DataStore2 and I made horrible mistake here.
Try to look on this post how to Increment value with DataStore2.
Post: How I Implemented DataStore2 Into My Game (And How You Could Too)