I would like to have a working datastore system within my Roblox game but for some reason the script isn’t working properly. It saved the data from a few days and now it’s only loading that data and not the most recently saved data, unless the most recently saved data was that from a few days ago. I can’t figure out whether it’s not saving properly or loading properly.
TL;DR; My datastore script either isn’t saving or loading data correctly, please help! I am an amateur scripter so I don’t really know how this works, I just used it from a YT tutorial.
Here is the script:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(Player)
wait()
local Playerkey = "id_"..Player.UserId
local save1 = Player.leaderstats.Coins
local save2 = Player.leaderstats.Gems
local save3 = Player.leaderstats.Level
local save4 = Player.leaderstats.Exp
local save5 = Player.QuestNum
local save6 = Player.Codes.Code1
local save7 = Player.Codes.Code2
local save8 = Player.Codes.Code3
local GetSaved = ds:GetAsync(Playerkey)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
save3.Value = GetSaved[3]
save4.Value = GetSaved[4]
save5.Value = GetSaved[5]
save6.Value = GetSaved[6]
save7.Value = GetSaved[7]
save8.Value = GetSaved[8]
print("Player data successfully loaded for "..Player.Name)
else
local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value, save5.Value, save6.Value, save7.Value, save8.Value}
ds:GetAsync(Playerkey, NumberForSaving)
Player.leaderstats.Coins.Value = 0
Player.leaderstats.Gems.Value = 0
Player.leaderstats.Level.Value = 0
Player.leaderstats.Exp.Value = 0
Player.QuestNum.Value = 1
Player.Codes.Code1.Value = 0
Player.Codes.Code2.Value = 0
Player.Codes.Code3.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
ds:SetAsync("id_"..Player.UserId, {Player.leaderstats.Coins.Value, Player.leaderstats.Gems.Value, Player.leaderstats.Level.Value, Player.leaderstats.Exp.Value, Player.QuestNum.Value, Player.Codes.Code1.Value, Player.Codes.Code2.Value, Player.Codes.Code3.Value})
print("Player data successfully saved for "..Player.Name)
end)
If GetSaved is nil that means there is no saved data so you just set the values to the default starter values.
Heres a data store script to work off of:
--> Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
--> Data Store
local DataStore = DataStoreService:GetDataStore("UserData")
--> Functions
local function GetPlayerData(player: Player)
local succ, data = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
return data
end
local function PlayerAdded(player: Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
local SavedData = GetPlayerData(player)
if SavedData ~= nil and typeof(SavedData) == "table" then
Coins.Value = SavedData[1]
elseif SavedData == nil or typeof(SavedData) ~= "table" then
--> Set New Player Values
end
Coins.Parent = leaderstats
leaderstats.Parent = player
end
local function PlayerRemoving(player: Player)
local leaderstats = player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Coins")
local Tbl = {
Coins.Value,
}
pcall(function()
DataStore:SetAsync(player.UserId, Tbl)
end)
end
--> Connections
for _, v in pairs(Players:GetPlayers()) do
PlayerAdded(v)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)
It doesn’t work. I tried it how you gave it to me and I even added another value to it and it doesn’t work. Here is the edited script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("UserData")
local function GetPlayerData(Player)
local success, data = pcall(function()
return DataStore:GetAsync(Player.UserId)
end)
return data
end
local function PlayerAdded(Player)
local leaderstats = Player.leaderstats
local Coins = leaderstats.Coins
local Gems = leaderstats.Gems
local SavedData = GetPlayerData(Player)
if SavedData ~= nil and typeof(SavedData) == "table" then
Coins.Value = SavedData[1]
Gems.Value = SavedData[2]
elseif SavedData == nil or typeof(SavedData) ~= "table" then
Coins.Value = 0
Gems.Value = 0
end
Coins.Parent = leaderstats
Gems.Parent = leaderstats
leaderstats.Parent = Player
end
local function PlayerRemoving(Player)
local leaderstats = Player:FindFirstChild("leaderstats")
local Coins = leaderstats:FindFirstChild("Coins")
local Gems = leaderstats:FindFirstChild("Gems")
local Tbl = {Coins.Value, Gems.Value,}
pcall(function()
DataStore:SetAsync(Player.UserId, Tbl)
end)
end
for _, v in pairs(Players:GetPlayers()) do
PlayerAdded(v)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)