Save money not work

hello developper!

i need you

my script does not work because

look at my picture:
image

my script:

local DataStore = game:GetService("DataStoreService")

local myDataStore = DataStore:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0

	local cc = Instance.new("IntValue",plr)
	cc.Name = "CashCollected"

	local robbing = Instance.new("BoolValue",plr)
	robbing.Name = "Robbing"
	robbing.Value = false


	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetASync(plr.UserId.."-Coins")
	end)

	if success then
		Coins.Value = data
	else
		print("There was an error while giving Player Data.")
		warn(errormessage)
	end

end)

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

	local success, errormessage = pcall(function()

		myDataStore:SetASync(plr.UserId.."-Coins", plr.leaderstats.Coins.Value)
	end)


	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error while saving Player Data.")
		warn(errormessage)
	end

end)


thank you in advance :slight_smile:

2 Likes

Try replacing

myDataStore:SetASync(plr.UserId.."-Coins", plr.leaderstats.Coins.Value)

With

myDataStore:SetAsync(plr.UserId.."-Coins", plr.leaderstats.Coins.Value)

:arrow_down: As @JackscarIitt Said :arrow_down:

1 Like

Replace

data = myDataStore:GetASync(plr.UserId.."-Coins")

with

data = myDataStore:GetAsync(plr.UserId.."-Coins") , 

same thing for the SetAsync as well

2 Likes

ah thank you I wrote wrong: SetAsync()
but my output…
image

Have you enabled API Services in Game Settings?

1 Like

As @JackscarIitt said, you need to publish the game and enable API services in order for DataStore to function in Studio.

1 Like

thank you!! thank you so much! :slight_smile:

1 Like