How Come My DataStore is not Working?

Hey I’m making a DataStore script and when I leave and rejoin in roblox it isn’t working. Here’s the script

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("TimeData")


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

	local Minutes = Instance.new("IntValue")
	Minutes.Name = "Minutes"
	Minutes.Parent = leaderstats

	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-minutes")
	end)
	if success then 
		Minutes.Value = data
	else 
		print("Error")
		warn(errormessage)
	end

	while task.wait(1) do 
		Minutes.Value += 1
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playeruserId = "Player_"..player.UserId

	local data = {
		Minutes = player.leaderstats.Minutes.Value
	}

	local success, errormessage = pcall(function()
		dataStore:SetAsync(playeruserId, data)
	end)

	if success then
		print("Successfuly saved data!")
	else
		print("There was an error in saving!")
		warn(errormessage)
	end
end)

All help appreciated

I reviewed your code and I am just wondering why you are putting, “data = dataStore:GetAsync(player.UserId…”-minutes")".

1 Like

Do you have a screenshot of the output?

1 Like

I see that you are loading data. I suggest you remove the .UserId…"-minutes". if success then Minutes.Value = data: You need to have a saving code. Don’t forget, you have to save data to load data. In the if statement above, you are basically setting the value of Minutes to your player ID. What you need to do instead is make a script that saves, and have a table called Data. In there, you must add a Minutes value and set it to the leaderstats minutes value. I will give you a code example below if that helps.

1 Like
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")

game.Players.PlayerRemoving:Connect(function(player)
	local playeruserId = "Player_"..player.UserId

	local data = {
		Minutes = player.leaderstats.Minutes.Value
	}

	local success, errormessage = pcall(function()
		datastore:SetAsync(playeruserId, data)
	end)

	if success then
		print("Successfuly saved data!")
	else
		print("There was an error in saving!")
		warn(errormessage)
	end
end)

This right here is the saving code. As you can see, there is a data table. Inside, is the value of Minutes, with its leaderstats value. What you need to do is go to your loading code, and put if success then Minutes.Value = data.Minutes If this doesn’t work, please let me know. Copy the code above into below your playeradded function.

1 Like

I suggest you put the leaderstats and Minutes variables inside playeradded. When I test with 3 players, only 1 player’s number is counter while the other two got nothing except a blank intvalue. Also the while loop inside the playeradded.

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("TimeData")


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

	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-minutes")
	end)
	if success then 
		Minutes.Value = data
	else 
		print("Error")
		warn(errormessage)
	end
	
	while task.wait(60) do 
		Minutes.Value += 1
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-minutes", plr.leaderstats.Minutes.Value)
	end)

	if success then
		print("Succesfully Saved Players Data!")
	else 
		print("Error Saving Player Data!")
		warn(errormessage)
	end
end)


1 Like

nothing showed up but anyways imma try the script @EnjoyableDeveloping suggested

This part here got an error

I tried removing the Minutes on the end and it doesn’t work

Try and print data and Minutes.Value, what does it return?

1 Like

It returned with
image
first number is data 2nd is Minutes.Value

Seems to be that the data is not loading properly, since nothing is there. Do you actually have anything there? Try testing it in-game?

The minutes go up by 1 when I do test it if that’s what you mean.
image

No, test loading data store in-game.

How would I go about doing that? By playing in roblox?

Do you have DataStoreService enabled in Roblox Studio? If not then enable it, because that might be the issue. Also run this script in the command bar to set the datastore for testing. If it loads it, then your good.

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetDataStore("TimeData")
WinsLeaderboard:SetAsync("1423303857-minutes",2034850)
1 Like

It’s enabled so I don’t think that’s the issue

Have you tried using DataStore2? Seems like what you try to save is very simple, so DataStore2 would be optional for you, if you do not have much experience with DataStores

Have you tried forcefully setting your datastore and see if it can load it?

local DataStoreService = game:GetService("DataStoreService")
local WinsLeaderboard = DataStoreService:GetDataStore("TimeData")
WinsLeaderboard:SetAsync("1423303857-minutes",2034850)

Do you want me to replace my variables with those? I dont understand how the second one would work if so

No, just run that in the Roblox Studio Command Bar.