Not saving Data! [DataStore]

  1. What do you want to achieve? Keep it simple and clear!
  2. What is the issue? Include screenshots / videos if possible!
    Data isn’t saving and no errors in output.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
local event = game.ReplicatedStorage:WaitForChild("coin")

local datastore = game:GetService("DataStoreService")

local myData = datastore:GetDataStore("myData")
--local GateData = datastore:GetDataStore("GateData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	
	local gate1 = Instance.new("BoolValue", plr)
	--gate1.Value = false
	gate1.Name = "Gate1"
	
	print("Boolean Status :",gate1.Value)
	
	local data
	local success,err = pcall(function()
		data = myData:GetAsync(plr.UserId)
	end)
	
	if success then
		coins.Value = data
		--gate1.Value = data
	end
	
	local function clockTime()
		event:FireClient(plr)
		coins.Value += math.random(1,100)
	end
	
	while wait(25) do
		clockTime()
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
	end)
	if not success then
		warn(err)
	end
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
	end)
	if not success then
		warn(err)
	end
end)
1 Like

Hello… again!

Try changing

local data
local success,err = pcall(function()
	data = myData:GetAsync(plr.UserId)
end)

to

local success,data = pcall(function()
	return myData:GetAsync(plr.UserId.."Coins")
end)
1 Like

At first everything was fine, but again it does not work…

Did you add more to it? If so let me see whatever you changed.

local event = game.ReplicatedStorage:WaitForChild("coin")

local datastore = game:GetService("DataStoreService")

local myData = datastore:GetDataStore("myData")
--local GateData = datastore:GetDataStore("GateData")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	
	local gate1 = Instance.new("BoolValue", plr)
	--gate1.Value = false
	gate1.Name = "Gate1"
		
	local success,data = pcall(function()  -- I CHANGE THIS
		return myData:GetAsync(plr.UserId.."Coins")
	end)
	
	if success then
		coins.Value = data
		--gate1.Value = data
	end
	
	local function clockTime()
		event:FireClient(plr)
		coins.Value += math.random(1,100)
	end
	
	while wait(25) do
		clockTime()
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
	end)
	if not success then
		warn(err)
	end
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
	end)
	if not success then
		warn(err)
	end
end)

What is the remote event in this doing?

Also, any errors or warns?

My current theory is you’re using :WaitForChild() which would be fine but it’s taking too long to find the child and by the time it finds it/gives up your player has already loaded but the function hasn’t, so it doesn’t run.

Every 25 seconds, the function is launched along with a remote event. The local script accepts and within 5 seconds the player runs faster than the previous speed.

Also no errors.

1 Like

Got it.

One last question, why is clocktime() a function? Couldn’t you just put the code in there in the while loop or do you need it as a function for future use?

It’s just an interest, I wanted to do that. But I don’t think it can be an error.

Not sure what you mean but I know the overall goal so here’s what I came up with.
first get rid of this line at the top:

local event = game.ReplicatedStorage:WaitForChild("coin")

then change

local function clockTime()
	game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
	coins.Value += math.random(1,100)
end

while wait(25) do
	clockTime()
end

to

while task.wait(25) do
	game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
	coins.Value += math.random(1,100)
end

This has a bit of a flaw though, we’re waiting 25 seconds AND the amount of time we wait for “coin”. If “coin” actually exists then this isn’t much of an issue because we’re only waiting a very small amount of extra time, but overall waiting can be a very small bit unreliable, if you wanted something more concrete you should look into RunService.

1 Like

I will definitely take a look at this service. But still, this will not solve the problem with preservation.

1 Like

Preservation of?

I’m a bit confused on the goal and problem

Data isn’t saved. But thanks for another helping.

1 Like

If you’re pressing the stop button in studio the data doesn’t save. Try leaving like you would a regular roblox game and see if it saves then.

1 Like

Yes, I forgot a little that the studio does not save data. Everything is good, works perfectly. But something Boolean is not preserved…

Maybe I’m not saving correctly when exiting?

1 Like

In your original code you have the boolean saving commented out.
Here’s the full script, everything should be working in it.

local datastore = game:GetService("DataStoreService")

local myData = datastore:GetDataStore("myData")

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

	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"

	local gate1 = Instance.new("BoolValue", plr)
	gate1.Name = "Gate1"

	local success,data = pcall(function()  -- I CHANGE THIS
		return myData:GetAsync(plr.UserId.."Coins")
	end)
	if success then
		coins.Value = data
	end

	local success,data = pcall(function()  -- I CHANGE THIS
		return myData:GetAsync(plr.UserId.."Gate1")
	end)
	if success then
		gate1.Value = data
	end


	while task.wait(25) do
		game.ReplicatedStorage:WaitForChild("coin"):FireClient(plr)
		coins.Value += math.random(1,100)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
	end)
	if not success then
		warn(err)
	end
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
	end)
	if not success then
		warn(err)
	end
end)

Double check that studio isn’t causing this issue because sometimes data doesn’t save through studio due to the server closing at the same time as the client when you end a play test.
I’ve recently had this issue and another way to solve it is by running a local server test with just one player and leaving the game but not closing the server and wait for it to save.

1 Like

Strangely, the Boolean is still not preserved.

change the value on the server-side

Value changing on the server script