Hello, I am having some troubles with Datastores saving. And Loading
What do you want to achieve? Keep it simple and clear!
For my datastore it won’t save or load any data. I have tried to make this, I used a tutorial but it was for leaderstat. So I tried to change it to just values.
What is the issue?
Data Store not saving/loading
What solutions have you tried so far? I have tried to use prints to see whats wrong and I can’t fix it.
Nothing in the dev console about it. And it has to be DataStore2, DataStore 1 doesn’t work.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore2")
game.Players.PlayerAdded:Connect(function(player)
local meat = Instance.new("IntValue")
meat.Name = "Meat"
meat.Value = 1000
meat.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 1000
money.Parent = player
local gunInventory = Instance.new("Folder")
gunInventory.Name = "GunInventory"
gunInventory.Parent = player
local skinsInventory = Instance.new("Folder")
skinsInventory.Name = "SkinInventory"
skinsInventory.Parent = player
local equippedGun = Instance.new("StringValue")
equippedGun.Name = "EquippedGun"
equippedGun.Parent = player
local equippedSkin = Instance.new("StringValue")
equippedSkin.Name = "EquippedSkin"
equippedSkin.Parent = player
local equippedAbilites = Instance.new("StringValue")
equippedAbilites.Name = "EquippedAbilites"
equippedAbilites.Parent = player
local data
local success, errorMsg = pcall(function()
data = DataStore:GetAsync(player.UserId)
end)
if data ~= nil then
-- Needs to load:
if data.equippedGun then
equippedGun.Value = data.EquippedGun
end
if data.equippedSkin then
equippedSkin.Value = data.EquippedSkin
end
if data.Meat then
meat.Value = data.meat
end
if data.Money then
money.Value = data.money
end
if data.equippedAbilites then
equippedAbilites.Value = data.money
end
if data.Skins then
for i,v in pairs(data.Skins) do
local val = Instance.new("StringValue")
val.Name = v
val.Parent = skinsInventory
end
end
if data.Guns then
for i,v in pairs(data.Guns) do
local val = Instance.new("StringValue")
val.Name = v
val.Parent = gunInventory
end
end
end
game.ReplicatedStorage.SendData:FireClient(player,data)
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {} -- Meat, Money, Skins/guns they own all get saved in here
data.Meat = player.Meat.Value
data.Money = player.Money.Value
data.equippedSkin = player.equippedSkin.Value
data.equippedGun = player.equippedGun.Value
data.equippedAbilites = player.equippedAbilites.Value
data.Skins = {}
data.Guns = {}
for i,v in pairs(player.SkinInventory:GetChildren()) do
table.insert(data.Skins,v.Name)
print(v.Name)
end
for i,v in pairs(player.GunInventory:GetChildren()) do
table.insert(data.Guns,v.Name)
print(v.Name)
end
local success,errorMsg = pcall(function()
DataStore:SetAsync(player.UserId,data)
end)
if success then
print("Successfully Saved!")
end
end)
It’s most likely not functioning because ROBLOX Studio is closing before it has time to execute all the code in your function. You wouldn’t have to worry about it in a in-game server. But in studio, it’s really a hit or miss. Especially because you’re running two for loops and a pcall method.
Here’s how to solve this issue:
game:BindToClose(function() -- Allow time for studio to execute the Datastore methods when the game closes.
if game:GetService("RunService"):IsStudio() then -- This wait() code will only occure in studio, not in a real server.
wait(3);
end
end)
This basically binds a function to the game, which fires when the game is closing. You can then check if the game is being run through ROBLOX Studio. If so, delay the shutdown to give enough time for Studio to execute the PlayerRemoving method.
You can also implement this for your in-game servers if you worry that the server will shutdown too quickly after the final player has left. Just to give enough time for any data saving.
(P.S) When stopping the studio test, it may freeze for a few seconds. It’s expected behavior. Since it’s finalizing a few processes before closing.
Would this script go at the top of it?! And also can I ask you 1 quick question about a very simple script, but I not very good at scripting so I can’t fix it.
I have this script, it sets a value to the text. (Pic down below) And it won’t show any value! It says textlabel is not a valued member of Image Button (or label).
local Meat = game.Players.LocalPlayer:FindFirstChild("Meat")
local MeatDisplay = script.Parent.Parent.MeatDisplay
script.Parent:WaitForChild("MeatDisplay").MouseButton1Click:Connect(function()
MeatDisplay.Text = Meat.Value
end)
Meat.Changed:Connect(function()
MeatDisplay.Text = Meat.Value
end)
Also, the script doesn’t load the correct value. It says loaded! in the dev console, and the value is still not the value that I saved. No it doesn’t fix the problem.
Well, I am changing the meat value to 1000. (It starts at 0) and when I leave, and join back (In studio) it is still 0, I waited a little and it still didn’t change.
Ok, I don’t know what setup you have going on. It can be any reason of problems. For starters, there’s a problem with your variable naming. Remember, variables are case sensitive.
If I create a variable Test, it’s not the same as test. For example:
-- These are two different variables.
local Test = 1;
local test = 1;
In your PlayerRemoving method, you’re saving to the following variables:
data.Meat = player.Meat.Value -- Saving to Meat
data.Money = player.Money.Value -- Saving to Money
data.equippedSkin = player.equippedSkin.Value -- Saving to equippedSkin
data.equippedGun = player.equippedGun.Value -- Saving to equippedGun
data.equippedAbilites = player.equippedAbilites.Value -- Saving to equippedAbilities
But then, you’re loading variables that don’t exist because you’re not taking into account the case sensitive naming. Which basically means you’re loading “nil” into these object values you created.
if data.equippedGun then
equippedGun.Value = data.EquippedGun -- Saved as equippedGun
end
if data.equippedSkin then
equippedSkin.Value = data.EquippedSkin -- Saved as equippedSkin
end
if data.Meat then
meat.Value = data.meat -- Saved as Meat
end
if data.Money then
money.Value = data.money -- Saved as Money
end
if data.equippedAbilites then
equippedAbilites.Value = data.money -- Saved as equippedAbilities (data.money shouldn't be here?)
end