DataStorage Help

Wait, how do I put the leaderstats folder inside the player?

I tried it just now but it didn’t work.

Did I place it correctly?

You have to create it inside the Player via a script. If you need help doing this:

If you have trouble reading the API reference, tell me.

Ik how to create the leaderboard. I just don’t know how to add the datastore into the leaderboard.

1 Like

You don’t add the DataStore into the leaderboard, you add the values.

2 Likes

can you show me an example please?

1 Like
local dataStoreService = game:GetService("DataStoreService")
local player_data = dataStoreService:GetDataStore("WorldMiles-0.0.1")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local Points = Instance.new("IntValue", leaderstats)
	Points.Name = "Points"
	Points.Value = 0

	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,
		}
		player_data:SetAsync(plr.UserId,newData)
	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
	
	Players.PlayerRemoving:Connect(data_save)
end)

I tested this and it works fine.

1 Like

I cant see it on the leaderboard, I don’t know why. @yoshi1604

Updated the script, I tested it and it works fine. Check it. @UOME2K

1 Like

It still doesn’t work. Maybe I placed it in the wrong place. Can you show me where you put your script?

In the ServerScriptService, a regular script.

Perfect, it works, thanks a lot for you help. How can I make a command which adds the points? @yoshi1604

No problemo, please mark the answer so new people can see the solution directly.
You can add points by adding numbers to the Points value.

Points.Value = Points.Value + 100

I mean like a command like :give all 100 or something like that

If you need to make commands, check a YouTube tutorial on it.
Maybe this one
Or this one

Ok. How about making a GUI which displays how much points I have?

In the Gui, place a TextLabel, and inside a LocalScript, get the points value, and change the TextLabel’s Text property to the Points value.
Tutorial!

My youtube doesn’t work. Could you show me an example please? @yoshi1604

I can’t do all the work for you, or else you won’t learn how to script! Check the tutorials I sent you and you’ll get a better grip on how things work.

1 Like