Help With Datastore For Points?

I’ve watched tutorials after tutorials and I’ve struggled to make it work, I have api services on and I’ve wrote the code exactly and it doesn’t work even with hours of tutorials, is there any where i can look for help or other tutorials that explain it all fully ?

You can first start at ROBLOX’s actual Developer Wiki, which has an in-depth explanation on how Data Stores work and how you can access and use them here. There is also a Datastore post here on the devforum that explains it completely to help you understand from beginning to end: DataStores - Beginners to Advanced.

I hope this helps!

1 Like

Would you mind providing your current code? We will be able to give some insight as to possible reasoning as to why it’s not working.

1 Like

Ive got an attempt here that didnt work, this just broke the game

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“MyDataStore”)

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

local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats

local wins = Instance.new("NumberValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats

local data
local success, errorMessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId)
end)

if success then
	Points.Value = data
else
	warn(errorMessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
end)

if success then
	print("Data Saved")
else
	warn(errorMessage)
end

end)

game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick(“Server Closed”)
end

wait(2)

end)

Did you get any errors in the output? What kind of script is this in?

1 Like

When you say break your game, what is exactly happening? Also, are you testing this in Studio? There are times when PlayerRemoving doesn’t end up firing correctly in Studio, as the solo test session is terminated before the event has time to begin firing. To test if this is your issue, in the command bar in Studio, type game.Players.LocalPlayer:Kick(), this will allow the test session to remain running but the PlayerRemoving event to fire. After this, if you stop the test session if this was your issue, the designated data should of been saved and loaded in.

Other than that, another potential issue is how your saving these values. In studio, are you editing these values manually through the explorer to see if it saves? If so, are you doing so on the client or the server? The datastore will only save server sided changes rather than client!

1 Like

im not to sure as i removed this as it just broke, tomorrow ill test and check the output but if there was a mistake in that area of it.

1 Like

Okay, whenever you get the chance

1 Like

So would that script work inside of a normal game ?, i haven’t tried actually testing it out of studio

Actually, just realised the issue is probably within the variables looking at the code.

Right now, you have:

if success then
		Points.Value = data --replace "Points" with "points"
	else 
	local success, errorMessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId) -- change "player" to "plr"
	end)

If the studio test session is the issue, PlayerRemoving will fire in game, it’s really just in Studio the PlayerRemoving event is unreliable. However, I believe the things above are more than likely the cause. Here is your script but I changed the values for you just in case you like copy and pasting it more and can’t find the specific lines!

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")

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

	local points = Instance.new("NumberValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats

	local wins = Instance.new("NumberValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local data
	local success, errorMessage = pcall(function()
		data = myDataStore:GetAsync(plr.UserId)
	end)

	if success then
		points.Value = data
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
	end)

	if success then
		print("Data Saved")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetChildren()) do
		player:Kick("Server Closed")
	end

	wait(2)
end)

You don’t need to change player to plr as it is passed as ‘player’ in the function

No it isn’t , this is the PlayerAdded event I’m talking about:
game.Players.PlayerAdded:Connect(function(plr)

1 Like

Oh I didn’t realise, well spotted!

1 Like

But my variable is Points not points ? why would i need to change it ?

Hey there! I made a tutorial on making data stores a while back! Also something I’ve noticed throughout my time is studios doesn’t always save, if you don’t see a “disconnected” message when you stop testing in studios, the game won’t save no matter what, I suggest testing data stores in Roblox itself for more accurate results.

Anyways here’s my video:

Your variable is named points, looking at your script:

local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats

I think the confusion here is what you’ve named the points. You’ll notice here that you’ve done local points to represent the points variable, but you’ve named the actual name of the number value Points. You can still reference it as Points, but you’d have to provide an actual path to the value with plr.leaderstats.Points, since it’s not a variable, rather just the name of the NumberValue.

1 Like