Hello Everyone! I need your help please.
I cant seem to get my DataStore Working with tables and Was wondering if anyone can help me out with a script.
My Leaderstats Contains:
- Cash
- Energy
- OffTime
- Exp
I have a script for both leaderstats and DataStore but It doesnt seem to save and Im not sure how the game:BindToClose works. Script will be down below.
Script Below
local DataStoreService = game:GetService("DataStoreService")
local GameStore = DataStoreService:GetDataStore("GameStore")
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 = player.leaderstats
Cash.Value = 100
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Parent = player.leaderstats
Exp.Value = 0
local Energy = Instance.new("IntValue")
Energy.Name = "Energy"
Energy.Parent = player.leaderstats
Energy.Value = 0
local OffTime = Instance.new("IntValue")
OffTime.Name = "OffTime"
OffTime.Parent = player.leaderstats
OffTime.Value = 0
local playerUserId = "Player".. player.UserId
-- Load The Data
local data = GameStore:GetAsync(playerUserId)
local success, errormessage = pcall(function()
data = GameStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data.Cash
Exp.Value = data.Exp
OffTime.Value = data.OffTime
Energy.Value = data.Energy
-- set our data to Player Cash
else
print("New Player or Data not found!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player".. player.UserId
local data = {
Cash = player.leaderstats.Cash.Value;
Energy = player.leaderstats.Energy.Value;
Exp = player.leaderstats.Exp.Value;
OffTime = player.leaderstats.Offtime.Value;
}
local success, errormessage = pcall(function()
GameStore:SetAsync(playerUserId, data)
end)
if success then
print("Data saved when leaving! Succesful")
else
print("There was an error saving")
warn(errormessage)
end
end)
Sorry if I did this wrong I am kinda new to posting.
Thank You!
Could you please paste the script into the devforum post? I would rather not download a random lua file.
Will do Im just kind of new to posting on the dev forum so sorry if I do this wrong.
local DataStoreService = game:GetService("DataStoreService")
local GameStore = DataStoreService:GetDataStore("GameStore")
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 = player.leaderstats
Cash.Value = 100
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Parent = player.leaderstats
Exp.Value = 0
local Energy = Instance.new("IntValue")
Energy.Name = "Energy"
Energy.Parent = player.leaderstats
Energy.Value = 0
local OffTime = Instance.new("IntValue")
OffTime.Name = "OffTime"
OffTime.Parent = player.leaderstats
OffTime.Value = 0
local playerUserId = "Player".. player.UserId
-- Load The Data
local data = GameStore:GetAsync(playerUserId)
local success, errormessage = pcall(function()
data = GameStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data.Cash
Exp.Value = data.Exp
OffTime.Value = data.OffTime
Energy.Value = data.Energy
-- set our data to Player Cash
else
print("New Player or Data not found!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player".. player.UserId
local data = {
Cash = player.leaderstats.Cash.Value;
Energy = player.leaderstats.Energy.Value;
Exp = player.leaderstats.Exp.Value;
OffTime = player.leaderstats.Offtime.Value;
}
local success, errormessage = pcall(function()
GameStore:SetAsync(playerUserId, data)
end)
if success then
print("Data saved when leaving! Succesful")
else
print("There was an error saving")
warn(errormessage)
end
end)
1 Like
Here is how you would save everyones data with BindToClose, I found it from here
This isn’t my script its an example you can find anywhere
game:BindToClose(function()
-- if the current session is studio, do nothing
if RunService:IsStudio() then
return
end
print("saving player data")
-- go through all players, saving their data
local players = Players:GetPlayers()
for _, player in pairs(players) do
local userId = player.UserId
local data = playerData[userId]
if data then
-- wrap in pcall to handle any errors
local success, result = pcall(function()
-- SetAsync yields so will stall shutdown
dataStore:SetAsync(userId, data)
end)
if not success then
warn(result)
end
end
end
print("completed saving player data")
end)
2 Likes
Thank You. Do you know how the Tables work because I cant seem to get them right.
Well the way I like to save tables is, I save the table itself and everything inside. Here’s an example
local DS = game:GetService("DataStoreService"):GetDataStore("TableSaves")
game.Players.PlayerAdded:Connect(function(Player)
local myTable = {}
local TableData = DS:GetAsync(Player.UserId.."TableKey") -- Our table key
local GunData = DS:GetAsync(Player.UserId.."Gunkey") -- Our gun tool key
if TableData then
if GunData then
game.ReplicatedStorage.Gun:Clone().Parent = Player.StarterGear -- If there is the gun data
end
end
end)
game:BindToClose(function() -- Save with bind to close
for i,v in pairs(game.Players:GetPlayers()) do
local TData = DS:GetAsync(Player.User . . .
end
end)
I’m not going to put the rest so you can learn how to do it.
Edit: Of course you should wrap it in a pcall which I didn’t do in that example.
1 Like