Another datastore problem!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    New player’s time value to go up.
  2. What is the issue? Include screenshots / videos if possible!
    When a new player joins the game, their time value doesn’t go up.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Putting a while loop in different areas.

Code:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("MyDataStore")

local function savedata(player)

	local valuestosave = {
		player.leaderstats.Towers.Value;
		player.TowersCompleted.Tower1.Value;
		player.TowersCompleted.Tower2.Value
	}

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, valuestosave)
	end)

	if success then
		print("Data has been successfully saved!")
	else
		print("There has been an error when saving the data!")
		warn(errormessage)
	end

end

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

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

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

	local towerscompleted = Instance.new("Folder", player)
	towerscompleted.Name = "TowersCompleted"

	local tower1 = Instance.new("BoolValue", towerscompleted)
	tower1.Name = "Tower1"
	tower1.Value = false

	local tower2 = Instance.new("BoolValue", towerscompleted)
	tower2.Name = "Tower2"
	tower2.Value = false
	
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success then
		towers.Value = data[1]
		tower1.Value = data[2]
		tower2.Value = data[3]
	else
		print("Player has no data!")
	end
	
	while wait(1) do
		timein.Value = timein.Value + 1
	end

end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		savedata(player)
	end)
	
end)

game:BindToClose(function()

	for _, player in pairs(game.Players:GetPlayers()) do
		local success, errormessage = pcall(function()
			savedata(player)
		end)
	end

end)

Help please!

Whenever I tested in Studio, it worked when I removed your if success statement.
Does it error for you in game? I got an error “Attempt to index nil with number”

Well I didn’t actually remove the if success statement so I didn’t get any errors.

Well, I added an if statement checking for the data and time goes up. Also, you don’t save or get the value in the time (If it’s meant to be like that then it works).

Edit: Here’s what i changed with your playeradded event

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

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

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

	local towerscompleted = Instance.new("Folder", player)
	towerscompleted.Name = "TowersCompleted"

	local tower1 = Instance.new("BoolValue", towerscompleted)
	tower1.Name = "Tower1"
	tower1.Value = false

	local tower2 = Instance.new("BoolValue", towerscompleted)
	tower2.Name = "Tower2"
	tower2.Value = false

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

	if success then
		if data then
			towers.Value = data[1]
			tower1.Value = data[2]
			tower2.Value = data[3]
		else
			print("No data")
		end
	else
		print("Player has no data!")
	end

	while wait(1) do
		timein.Value = timein.Value + 1
	end

end)

Yea I don’t want it to save because it is just meant to be there to show how long the player has been in the game.

Okay, then that code should work fine. It worked whenever I tested it in studio.

The one that I originally had?

For the most part, yeah but I was referring to the snippet I sent of the Players.PlayerAdded event.
All I did was add a statement that checked if there was actually data that got loaded.

1 Like

Try multithreading; It’s good practice.


coroutine.resume(coroutine.create(function()
      while wait(1) do
          timein.Value += 1
      end
end))
1 Like