Hi.
I’ve tried reading about DataStores, then trying to make one myself. No luck. It doesn’t work at all. Sometimes it gives an error, and sometimes it doesn’t. I’ve been trying for about a whole day now and it’s extremely annoying. I’ve asked before and not much helped. I always had to go back to square one, either because my entire game broke or nothing worked.
I don’t know what I’m doing wrong.
I want to save these stats and load them when the player joins.
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local statistics = Instance.new("Folder",player)
statistics.Name = "statistics"
statistics.Parent = player
local moneyStat = Instance.new("IntValue",leaderstats)
moneyStat.Name = "Money"
moneyStat.Value = 0
moneyStat.Parent = leaderstats
local multiplierStat = Instance.new("IntValue",leaderstats)
multiplierStat.Name = "Multiplier"
multiplierStat.Value = 1
multiplierStat.Parent = leaderstats
local maxAmountStat = Instance.new("IntValue",statistics)
maxAmountStat.Name = "RandomAmountMax"
maxAmountStat.Value = 5
maxAmountStat.Parent = statistics
local minAmountStat = Instance.new("IntValue",statistics)
minAmountStat.Name = "RandomAmountMin"
minAmountStat.Value = 1
minAmountStat.Parent = statistics
Where’s the DataStore variables? Where’s the protective call for GetAsync? I don’t see you’re learning the basics of DataStore, I recommend you just watch a tutorial.
First, if you haven’t already, enable studio access to API services (Game settings, Security)
What you need to do is create a datastore for each one using DataStoreService (Just going to use your moneyStat as an example) and then tell it to place whatever value that is in the datastore to your moneyStat.
local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("Money")
local function onPlayerJoin(player)
local moneyStat = Instance.new("IntValue",leaderstats)
moneyStat.Name = "Money"
moneyStat.Value = 0
moneyStat.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = moneyDataStore:GetAsync(playerUserId)
end)
if success then
player.Money.Value = data
print("Successfully loaded player data!")
else
print("Error loading in a player's data")
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Also please correct me if any of the code is wrong
Then you would just create multiple datastores with each stat that you want:
local moneyDataStore = DataStoreService:GetDataStore("Money")
local statisticsDataStore = DataStoreService:GetDataStore("statistics")
local multiplierDataStore = DataStoreService:GetDataStore("Multiplier")
local maxAmountDataStore = DataStoreService:GetDataStore("RandomAmountMax")
local minAmountDataStore = DataStoreService:GetDataStore("RandomAmountMax")
and then you can modify the pcall function to be:
local data
local data2 -- etc.
local success, errormessage = pcall(function()
data = moneyDataStore:GetAsync(playerUserId)
data2 = statisticsDataStore:GetAsync(playerUserId)
-- Basically just repeat this for each datastore (data3 = (datastore):GetAsync(playerUserId), data4, data5...)
-- Or you can just set up a table of the values like Vong25 said
end)
if success then
player.Money.Value = data
player.statistics.Value = data2
-- You can also just repeat it for each value
print("Successfully loaded player data!")
else
print("Error loading in a player's data")
end
end
But that is just to set the data when the player joins.
Right now the data doesnt get saved when the player leaves so in order to do that:
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.Money.Value
local data2 = player.statistics.Value
-- Pretty much just repeat for each value again
-- Or you can just set up a table of the values like Vong25 said
local success, errormessage = pcall(function()
moneyDataStore:SetAsync(playerUserId, data)
statisticsDataStore:SetAsync(player.UserId, data)
-- and also repeat for each datastore too.
end)
if success then
print("Successfully saved data")
else
print("Error saving data")
end
end
)
There’s probably a better way than what I am doing.
Hello! DataStores are an ever growing thing ROBLOX uses to help players save player data in game. Tables are usually the best way to do it!
But, thankfully, there have been methods of DataStores that are easy to use, and simple to operate with! I will provide you with some helpful links to checkout! These links are what helped me, and will help you if you put the time in to read them!
Right. Woops. Though, what else happens is that all of my stats get set to 0 by default. Even when I have such things as a Multiplier set to 1 by default.
It took me a few months to get datastores working properly. Datastores are tricky because of all the assumptions that tutorials have of your understanding of the API they provide. That’s the reason why so many people opt for a premade datastore handler that is easier to use like DataStore2 or ProfileService which do the tricky stuff for you.
It takes time getting used to it if you are doing it on your own. It might be helpful looking into how DataStore2 and ProfileService implement things (though I have to admit it is confusing to read through).
One thing I noticed in the thread that hasn’t been taken into account is initialized data. If a player has never saved before, their data is nil. You have to check for that, and if you find a nil value, set it equal to the default value. Otherwise you are resetting the default value to nil which I would think has undefined behaviour but apparently it is setting everything to 0 by default for you.