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