What do you want to achieve? Keep it simple and clear!
Datastore system that will save player’s cash
What is the issue? Include screenshots / videos if possible!
The script is working but it won’t save the cash when leave the game
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried to search on how to fix this but still I don’t know how. I’m just a beginner to scripting and don’t know much about datastore so I just use an open source script and tutorial I found.
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 cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = MyDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error whilst getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
MyDataStore:GetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error saving your data")
warn(errormessage)
end
end)
Hello, the code below SHOULD work… but imma give a little context for ya so you understand why!
basically in your game settings theres a thing calls “Allow studio access to API services.” basically you need that ON. when its off, basically it doesnt allow ROBLOX to connect to the data stores initially. so turning that on would allow you to.
now that that problem is solved, now you wanna use SetAsync() instead of get async at the end. Setting it on the playerremoving basically sets there data store value to there current data. Thats setting the async, and thats what you want it to do. BUT if you use GET async on player removing then the script just gets the values and doesnt set it nor save it. it just gets it. you want it to SET it. i hope i explained this well!
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 cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = MyDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error whilst getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
MyDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("Player data successfully saved!")
else
print("There was an error saving your data")
warn(errormessage)
end
end)