Datastore2 | Find "player" with create datastore

Hello !
Its been 2 days I use datastore2 instead of the original one. So i make a lot of errors haha
My problem is when i click on a textbutton, and try to find the player ( for giving him money ), its not working :
this is my script in server script service

Players.PlayerAdded:Connect(function(player)
	local currencyStore = DataStore2("currency", player)
	
	local function updateClientCurrency(amount)
		replicatedStorage.data.update:FireClient(player, amount)
	end
	
	updateClientCurrency(currencyStore:Get(defaultValue))	
	
	currencyStore:OnUpdate(updateClientCurrency)
	
end)

and my script inside my textbutton

local ServerScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local DataStore2 = require(ServerScriptService.DataStore2)
local button = script.Parent

button.MouseButton1Click:Connect(function(player)
	local currencyStore = DataStore2("currency", player)

	currencyStore:Increment(10)
	print("aled")

end)

And my error is :
image

The remote event wasn’t right…
Try this:

Server Script

local DataStore2 = require(1936396537)
local defaultValue = 0
local replicatedStorage = game.ReplicatedStorage
DataStore2.Combine("DATA", "Currency")
game.Players.PlayerAdded:Connect(function(player)
	local currencyValue = Instance.new("IntValue",player); currencyValue.Name = "currency"
	local currencyStore = DataStore2("currency", player)
	
	local function updateClientCurrency(amount)
		player.currency.Value = amount
	end
	updateClientCurrency(currencyStore:Get(defaultValue))	
	currencyStore:OnUpdate(updateClientCurrency)

end)

game.ReplicatedStorage.data.update.OnServerEvent:Connect(function(player)
	local currencyStore = DataStore2("currency", player)
	currencyStore:Increment(10)
end)

Then put this inside the textbutton in a “LOCAL SCRIPT”


local replicatedStorage = game:GetService("ReplicatedStorage")

local button = script.Parent

button.MouseButton1Click:Connect(function(player)

replicatedStorage.data.update:FireServer()

print("aled")

end)

Make sure api services are enabled or publish the game and test.

1 Like

THANK YOU A LOT <3 it works ! really

1 Like