Setting the amount of coins a user with specific ID using DataStore2

Hi, I’m trying to make it so that if a specific user joins, they will have 999,999 coins. Here is my code:

local DataStore2 = require(game.ServerScriptService.DataStore2)
local MarketplaceService = game:GetService("MarketplaceService")

local defaultCashValue = 0


game.Players.PlayerAdded:Connect(function(plr)
	local coinsDataStore = DataStore2("Coins", plr)
	
	local cash = Instance.new("StringValue")
	cash.Name = "Cash"
	
	
	local function coinsUpdated(updatedValue)
		cash.Value = coinsDataStore:Get(updatedValue)
		game.ReplicatedStorage.UpdateClientCurrency:FireClient(plr, coinsDataStore:Get(defaultCashValue))
	end
	
	coinsUpdated(defaultCashValue)
	
	coinsDataStore:OnUpdate(coinsUpdated)
	
	if plr.UserId == 34355831 then
		coinsDataStore:Set(999999)
	end



end)

what is the problem?
30char30char

No errors occur. The player doesn’t get the amount.

if plr.UserId == 34355831 then
		coinsDataStore:Set(999999)
coinsUpdated(999999)
	end

That did not work. It’s Set and get, instead of SetAsync and GetAsync.

game.Players.PlayerAdded:Connect(function(plr)
local coinsDataStore = DataStore2("Coins", plr)
local cash = Instance.new("StringValue")
cash.Name = "Cash"

local newValue = coinsDataStore:Get(100)

if plr.UserId == 34355831 then
	newValue= 999999
end
cash.Value = newValue
coinsDataStore:Set(newValue)
game.ReplicatedStorage.UpdateClientCurrency:FireClient(plr, newValue)

end)

What? That’s getting rid of the default value. That completely changes my Datastore.

All I’d like is to set an amount for a specific user who joins to ONLY that user.

yes, that will work for someone with that userid

It still did not work. It kept the same amount the user originally had.

the problem is when you do

coinsDataStore:Get(updatedValue)

you were giving the argument as a number

That’s how it works, ::Get() takes any argument and sets it as a default value when data doesn’t exist.

@WooleyWool try something like this


local DataStore2 = require(game.ServerScriptService.DataStore2)
local MarketplaceService = game:GetService("MarketplaceService")

DataStore2.Combine("MainKey", "Coins")
local defaultCashValue = 0


game.Players.PlayerAdded:Connect(function(plr)
	local coinsDataStore = DataStore2("Coins", plr)
	
	local cash = Instance.new("StringValue")
	cash.Name = "Cash"

	
	local function coinsUpdated(updatedValue)
		cash.Value = coinsDataStore:Get(updatedValue)
		game.ReplicatedStorage.UpdateClientCurrency:FireClient(plr, coinsDataStore:Get(defaultCashValue))
	end
	
	coinsUpdated(defaultCashValue)
	
	coinsDataStore:OnUpdate(coinsUpdated)
	
	if plr.UserId == 34355831 then
           local v = 99999
           coinsDataStore:Set(v)
           cash.Value = v
	end

 -- cash.Parent = player.leaderstats ? 

end)
1 Like