What do you want to achieve? I want to try and save a table to datastore.
What is the issue? 1. I have never worked with tables before interms of datastores. 2. I have no idea how to script one.
What solutions have you tried so far? I have looked around the dev forum and haven’t found anything that helped.
Here is the code I have tried based on my knownledge on saving to datastores
local MDS = game:GetService("DataStoreService"):GetDataStore("StatsIGuess")
game.Players.PlayerAdded:Connect(function(plr)
local Folder = Instance.new("Folder",plr)
Folder.Name = "leaderstats"
local IntValue1 = Instance.new("IntValue",Folder)
IntValue1.Name = "Money"
local IntValue2 = Instance.new("IntValue",Folder)
IntValue2.Name = "Diamonds"
local IntValue3 = Instance.new("IntValue",Folder)
IntValue3.Name = "Area"
local ID = plr.UserId.."-Values"
local savedData
pcall(function()
savedData = MDS:GetAsync(ID)
end)
if savedData ~= nil then
IntValue1.Value = table.find(savedData,nil,1)
IntValue2.Value = table.find(savedData,nil,2)
IntValue3.Value = table.find(savedData,nil,3)
else
IntValue1.Value = 0
IntValue2.Value = 0
IntValue3.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local ID = plr.UserId.."-Values"
pcall(function()
MDS:SetAync(ID,{
plr.leaderstats.Money.Value,
plr.leaderstats.Diamonds.Value,
plr.leaderstats.Area.Value,
})
end)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
if v then
v:Kick()
end
end
wait(5)
end)
I am mainly requesting help on the game.Players.PlayerRemoving function
Yes they work in studio. However, sometimes according to a few accounts. The first few times you test datastores in studio the datastores may not save data. However these are just from a few people.
Yes.
Here is the current script so you can go test it if you want to
local MDS = game:GetService("DataStoreService"):GetDataStore("StatsIGuess")
game.Players.PlayerAdded:Connect(function(plr)
local Folder = Instance.new("Folder",plr)
Folder.Name = "leaderstats"
local IntValue1 = Instance.new("IntValue",Folder)
IntValue1.Name = "Money"
local IntValue2 = Instance.new("IntValue",Folder)
IntValue2.Name = "Diamonds"
local IntValue3 = Instance.new("IntValue",Folder)
IntValue3.Name = "Area"
local ID = plr.UserId.."-Values"
local savedData
pcall(function()
savedData = MDS:GetAsync(ID)
end)
if savedData ~= nil then
IntValue1.Value = savedData[1]
IntValue2.Value = savedData[2]
IntValue3.Value = savedData[3]
else
IntValue1.Value = 0
IntValue2.Value = 0
IntValue3.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local ID = plr.UserId.."-Values"
print(1)
pcall(function()
print(2)
MDS:SetAsync(ID, {
plr.leaderstats.Money.Value,
plr.leaderstats.Diamonds.Value,
plr.leaderstats.Area.Value,
})
print(3)
end)
print(4)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
if v then
v:Kick()
end
end
wait(5)
end)
Hi I just wanted to let you know that you should use metatables to add some contingency plans just in case you can’t retrieve data in the table from your datastores. I understand it isn’t working currently but just for the future add a metatable. These allow you to return default values just in case you can’t get something from a table. I use metatables in my games for tables I save. When I can’t get the amount of coins from a player’s table that was saved because the data didn’t load properly, I use a metatable to return a default amount which is 0. Another example of me using a meta table is when a player first joins a game, the game can’t get values from the datastores since there is no saved value in the datastores yet. So what I do is I set a metatable to give players default values for stuff such as coins, levels, and XP.
Your code does save data and it loads up completely fine. Are you sure you have API services enabled? Go to game settings, Security and then Enable Studio Access to API Services.