Hey there!
I am trying to save multiple values in my data store but it is not working. This is a script in server script service, does anyone know why it is not saving? It says it is saving but it is not.
Here is the script:
local DataStoreService = game:GetService("DataStoreService")
local SavedData = DataStoreService:GetDataStore("SavedData")
game.Players.PlayerAdded:Connect(function(plr)
local Wins = Instance.new("IntValue")
local Cash = Instance.new("IntValue")
local leaderstats = Instance.new("Folder")
Wins.Name = "Wins"
Cash.Name = "Cash"
Cash.Parent = leaderstats
Wins.Parent = leaderstats
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local OwnedItems = Instance.new("Folder")
OwnedItems.Name = "OwnedItems"
OwnedItems.Parent = plr
local Saves = {}
Saves.Cash = Cash.Value
Saves.Wins = Wins.Value
local Id = plr.UserId
local Data
local success, errormessage = pcall(function()
Data = SavedData:GetAsync(Id)
end)
if success then
print("Successfully got old data!")
Cash.Value = Saves.Cash
Wins.Value = Saves.Wins
end
if errormessage then
print("An unexpected error has happened, data has not saved!")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local Data = {}
Data.Cash = plr.leaderstats.Cash.Value
Data.Wins = plr.leaderstats.Wins.Value
local Id = plr.UserId
local success, errormessage = pcall(function()
SavedData:SetAsync(Id, Data)
end)
if errormessage then
print("An unexpected error has occured, lets hope the autosaves worked!")
end
if success then
print("Data Saved!")
end
end)
Thank you!
When you load the data, you aren’t setting Cash.Value and Wins.Value to the values stored in the table which is received from SavedData:GetAsync(Id). In the success conditional, consider changing it to:
if success then
print("Successfully got old data!")
Cash.Value = Data.Cash
Wins.Value = Data.Wins
end
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Money") --change Money to your datastore name
local savecommand = "/save" --command that saves your data
game.Players.PlayerAdded:Connect(function(player)
local leaderstat = Instance.new("Folder")
leaderstat.Name = "leaderstat"
leaderstat.Parent = player
local Molah = Instance.new("IntValue") --Change Molah to your money name
Molah.Name = "Molah" -- change the name Molah to your money name
Molah.Value = 0
Molah.Parent = leaderstat
local key = player.UserId.."-money"
local data = DS:GetAsync(key)
if data then
Molah.Value = data
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == savecommand then
local money = player.leaderstat.Molah --change Molah to your moneys name
DS:SetAsync(player.UserId.."-money", money.Value)
end
end)
end)
Also in settings, Security enable HTTP and API Services
Add a function that gives you money then say your save command. once you rejoin or die it will set to that save file.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Money") --change Money to your datastore name
local savecommand = "/save" --command that saves your data
game.Players.PlayerAdded:Connect(function(player)
local leaderstat = Instance.new("Folder")
leaderstat.Name = "leaderstat"
leaderstat.Parent = player
local Molah = Instance.new("IntValue") --Change Molah to your money name
Molah.Name = "Molah" -- change the name Molah to your money name
Molah.Value = 0
Molah.Parent = leaderstat
local Win = Instance.new("IntValue")
Win.Name = "Wins"
Win.Value = 0
Win.Parent = leaderstat
local key = player.UserId.."-money"
local keyWin = player.UserId.."-Wins"
local dataWin = DS:GetAsync(keyWin)
if dataWin then
Win.Value = dataWin
end
local data = DS:GetAsync(key)
if data then
Molah.Value = data
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == savecommand then
local money = player.leaderstat.Molah --change Molah to your moneys name
local Wins = player.leaderstat.Win
DS:SetAsync(player.UserId.."-money", money.Value)
DS:SetASync(player.UserId.."Wins", Wins.Value)
end
end)
end)
You still have to change molah value but i set up the Wins