My datastore script isnt working

I have a datastore that is supposted to save data (coins) from my game but it only works sometimes, heres the code:

local DataStoreService = game:GetService(“DataStoreService”)
local CoinsStore = DataStoreService:GetDataStore(“CoinsStore”)

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder") --making leaderstats
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

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



local UserId = player.UserId

local data

local success, errormessage = pcall(function() --getting loaded data
	data = CoinsStore:GetAsync(UserId)
end)

if success then
	Coins.Value = data
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local data = player.leaderstats.Coins.Value

CoinsStore:SetAsync(UserId, data)

end)

– The coin itself is in a folder called “Coins” in workspace

Make it print something if it got the data successfully, and make it print something else if it didn’t (known as debugging). Also, make sure to use pcall for :SetAsync() as well, and do the same printing for that as well.

After you do that, please tell us the results.

1 Like

Where should i write down set async?

Code:

Edit: I updated this with the correct quotation marks.

local DataStoreService = game:GetService("DataStoreService")
local CoinsStore = DataStoreService:GetDataStore("CoinsStore")

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

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



local UserId = player.UserId

local data

local success, errormessage = pcall(function() --getting loaded data
	data = CoinsStore:GetAsync(UserId)
end)

if success then
	Coins.Value = data
print("Got data successfully")
else
print(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local UserId = player.UserId
local data = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
CoinsStore:SetAsync(UserId, data)
end)
if success then
print("Saving successful")
else
print(errormessage)
end
end)

I provided the code.

Note:I am on mobile right now, so if there are errors please let me know.

1 Like

I believe you might have used incorrect quotation marks.

I think this worked i’m gonna check on my phone too and i’ll update u

1 Like

Pretty easy to make one without a leaderboard system its called API services you can enable it in your experience configurations

local DSS = game:GetService(“DataStoreService”)
local DataStore = DSS:GetDataStore(“DataStore”)
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new(“Folder”, plr)
leaderstats.Name = “leaderstats”

local money = Instance.new("IntValue", leaderstats)
money.Name = "Null"
money.Value = 0
local PUI = "Player_"..plr.UserId

local data
local success, errormessage = pcall(function()
	data = DataStore:GetAsync(PUI)
end)

if success then
	money.Value = data.money
end

end)

game.Players.PlayerRemoving:Connect(function(plr)
local PUI = “Player_”…plr.UserId

local data = {
	money = plr.leaderstats.money.Value;
}

local success, errormessage = pcall(function()
	data = DataStore:SetAsync(PUI, data) 
end)

end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.