DataStorage Help

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.

can you show us the actual datastore script?

1 Like

What do you mean? How can I view the actual datastore script?

the script ur showing is not a datastore script , datastore scripts have some sort of Async

1 Like

Oh. How can I use an async in my script?

If this is the full code, you’re missing to actually create the DataStorage. I’d suggest checking the DataStorage API reference:

If you do have code using this, please show it!

1 Like

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?

Alright, give me a minute and I’ll send some help.

OHH mb i thought u made a data store and wanted us to review it

1 Like

I didn’t mean that. I just wanted help to add a datastore, thats all.

First of all, let’s create our DataStorage:

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.

That has helped me alot. But how would I add multiple values, like Points and Status Credits for that person.

Add these values you want to save to the tables and you should be good to go.
By the way

You should use IntValues to store numbers, it’s a better practice.

Where in the datastorescript should I add this code?

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.

Can you send me the full script?

1 Like
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.

1 Like

Thanks! It works. But the data isnt displaying as a leaderboard.

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?