Hello All, I have created an airline frequent flyer miles system. But to fully finish it, I need some help trying to save the points data. I try to make a datastore but it doesn’t seem to work. I have looked into tutorials on how to save my data. But none of them help me at all. Please review my code and please tell me what I should add, etc.
local playerStats = {} --this keeps a list of the stats for each player that enters the game
game.Players.PlayerAdded:connect(function(player) --Added function
local leaderstats = Instance.new("Model", player)
leaderstats.Name = "leaderstats"
local Points = Instance.new("StringValue", leaderstats)
Points.Name = "Points"
Points.Value = 0
local grouprank = Instance.new("StringValue", leaderstats)
grouprank.Name = "Red Air"
local rank = player:GetRoleInGroup(8373718)
local rankn = player:GetRankInGroup(8373718)
grouprank.Value = rank
if rankn == 75 then
local team = game:GetService("Teams")
player.TeamColor = team.DJ.TeamColor
playerStats[player] = leaderstats
end
end)
I’d appreciate it if someone could help me solve this.
As I said in my description. The Developer API Reference is too difficult for me to understand. So Ill need specific steps to help me. How do I create the datastore?
local player_data = dataStoreService:GetDataStore("YourData-0.0.1")
Now that we have our DataStore, why don’t we create our data?
local newData = {
["Example"] = 0,
}
player_data:SetAsync(plr.UserId,newData)
There we have it, we created this table.
Now what? Should we load the data? There’s a lot of ways to do this, but I like to do this: (With some edits, this could be your final code:)
local data = player_data:GetAsync(plr.UserId)
if data ~= nil then
Example.Value = data["Example"] -- we're setting the data if there is data to use.
else
local newData = { -- we create new data if it doesn't exist!
["Example"] = 0,
}
end
If you need to save data:
local function data_save() -- let's create a saving function
local data = { -- getting our data.
["Example"] = Example.Value,
}
local success,err = pcall(function() -- a pcall to check if the data saves or not
player_data:SetAsync(plr.UserId, data) -- saving our data
end)
if success then
print("Data saved!")
else
print("Data didn't save!")
end
end
-- You'd just have to connect this when you want to save data.
This should be enough to get you started, but again, you should read the API reference, you have to learn how to read it.
local player_data = dataStoreService:GetDataStore("WorldMiles")
local newData = {
["Points"] = 0,
}
player_data:SetAsync(plr.UserId,newData)
local data = player_data:GetAsync(plr.UserId)
if data ~= nil then
Points.Value = data["Points"] -- we're setting the data if there is data to use.
else
local newData = { -- we create new data if it doesn't exist!
["Points"] = 0,
}
end
local function data_save() -- let's create a saving function
local data = { -- getting our data.
["Points"] = Points.Value,
}
local success,err = pcall(function() -- a pcall to check if the data saves or not
player_data:SetAsync(plr.UserId, data) -- saving our data
end)
if success then
print("Data saved!")
else
print("Data didn't save!")
end
end
-- You'd just have to connect this when you want to save data.
@yoshi1604
I used your code but it didn’t seem to work.
local player_data = dataStoreService:GetDataStore("WorldMiles")
local newData = {
["Points"] = 0,
}
player_data:SetAsync(plr.UserId,newData)
local data = player_data:GetAsync(plr.UserId)
local Points = Instance.new("StringValue", leaderstats)
Points.Name = "Points"
Points.Value = 0
if data ~= nil then
Points.Value = data["Points"] -- we're setting the data if there is data to use.
else
local newData = { -- we create new data if it doesn't exist!
["Points"] = 0,
}
end
local function data_save() -- let's create a saving function
local data = { -- getting our data.
["Points"] = Points.Value,
}
local success,err = pcall(function() -- a pcall to check if the data saves or not
player_data:SetAsync(plr.UserId, data) -- saving our data
end)
if success then
print("Data saved!")
else
print("Data didn't save!")
end
end
-- You'd just have to connect this when you want to save data.
You should remove the first new data, you’re already creating one below. And you should use your saving function, or else, the data won’t save. Again, I suggest changing the StringValue to an IntValue.
Well, if the IntValue is in the leaderstats folder, it should be appearing. If this is the full script, did you create a folder called "leaderstats" inside the player?