local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local cash = Instance.new("IntValue")
cash.Name = "Kronos"
cash.Parent = player
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsnyc(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("Failed to save data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:GetAsnyc(player.UserId.."-cash",player.Cash.Vaule)
end)
if success then
print("Data saved")
else
print("Failed to save data")
warn(errormessage)
end
end)
✓ ANSWER ACCEPTED
help !! there something wrong with my script . The script won't save data when i left the game ?
1 answer
✓ Accepted answer
You cannot set it to GetAsync when player is removing, as it won't save data. Instead use SetAsync to save at when player is removing
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local cash = Instance.new("IntValue")
cash.Name = "Kronos"
cash.Parent = player
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsnyc(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("Failed to save data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.Cash.Vaule)
end)
if success then
print("Data saved")
else
print("Failed to save data")
warn(errormessage)
end
end)
This should work
1 comment
0
thank
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Are you getting Error messages, or is it just not saving?
Im pretty sure that it just don't save
i get "Failed to save data"