hey, I am making a game that tracks your donations with a data store. However, when I try to save it, it doesnt work. I was able to pinpoint the issue down to a line but cant figure out the issue with it. here is my code:
local DSS = game:GetService("DataStoreService")
local spentstore = DSS:GetDataStore("spentstore")
game.Players.PlayerAdded:Connect(function(player)
local data = Instance.new("Folder")
data.Name = "leaderstats"
data.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Donations"
cash.Parent = data
local loadspent
local success, errormessage = pcall(function()
loadspent = spentstore:GetAsync(player.UserId.." spent")
end)
if success then
cash.Value = loadspent
else
print("error while recieving data")
warn(errormessage)
player:Kick("error loading data, please try again")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
print("the gaming fungus")
local success, errormessage = pcall(function()
spentstore:SetAsync(player.UserId.." spent",player.leaderstats.Donations.Value)
print("the gaming fungus")
end)
if success then
print("spent succesfully saved!")
else
print("spent saving failed!")
warn(errormessage)
end
end)
“the gaming fungus” only prints out once, so I can assume that the issue is with the SetAsync function. Any help?
Just to make sure, are you testing it in an actual game? Datastores do not work in Studio (EDIT: unless you have API access enabled like @colbert2677 mentioned)
Ok, I tested this script out and it didn’t work until I added a Bindtoclose function for some reason (might be because studio only works with BindToClose). It also started to print out the PlayerRemoving correctly.
local DSS = game:GetService("DataStoreService")
local spentstore = DSS:GetDataStore("spentstore")
game.Players.PlayerAdded:Connect(function(player)
local data = Instance.new("Folder")
data.Name = "leaderstats"
data.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Donations"
cash.Parent = data
local loadspent = nil
local success, errormessage = pcall(function()
loadspent = spentstore:GetAsync(player.UserId.." spent")
end)
if success then
cash.Value = loadspent
else
print("error while recieving data")
warn(errormessage)
player:Kick("error loading data, please try again")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
print("the gaming fungus")
local success, errormessage = pcall(function()
spentstore:SetAsync(player.UserId.." spent",player.leaderstats.Donations.Value)
print("the gaming fungus")
end)
if success then
print("spent succesfully saved!")
else
print("spent saving failed!")
warn(errormessage)
end
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
print("the gaming fungus")
local success, errormessage = pcall(function()
spentstore:SetAsync(v.UserId.." spent",v.leaderstats.Donations.Value)
print("the gaming fungus")
end)
if success then
print("spent succesfully saved!")
else
print("spent saving failed!")
warn(errormessage)
end
end
end)