Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why my datastore is not saving my added money?

I've made a datastore that saves money. The problem here is that when I click the button that gives me the money, the money gets added but when I exit and rejoin the money isn't there. I tested about adding the value through properties and the datastore saves that value. Here are the Scripts:

Leaderstats (script)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

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

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

	local data
	local success, errorMessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success then
		coins.Value = data
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
	end)

	if success then
		print("Data Saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetChildren()) do
		player:Kick("Server Closed")
	end

	wait(2)
end)

Button (localscript)

local Player = game.Players.LocalPlayer
local PlayerMoney = Player:WaitForChild('leaderstats'):WaitForChild('Coins')
script.Parent.MouseButton1Click:Connect(function()
PlayerMoney.Value += 1
end)

API is enabled too. I hope somebody helps me. Thank you in advance.

2 comments

0

Are you testing in studio. If you are, are you actually leaving the game or just stopping it?

0

I've tested both. Leaving and stopping the game.

1 answer

local key=""..player.UserId -- it needs to be a string

2 comments

0

where I've to put it?

0

On GetAsync & SetAsync : myDataStore:GetAsync(key) & myDataStore:SetAsync(key,player.leaderstats.Coins.Value)

This discussion is preserved as it was. New answers, comments, and votes are closed.