Why is my datastores not working?

Hello. I am trying to make a new leaderstat that goes up every second you play and I am unable to save it. I have read over the script multiple times, followed various tutorials, but nothing seems to work. Here is the script:

local data = game:GetService("DataStoreService")
local ds = data:GetDataStore("timeplayed")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaders = Instance.new("Folder")
	leaders.Name = "leaderstats"
	leaders.Parent = player
	
	local secsingame = Instance.new("IntValue")
	secsingame.Name = "PlayPoints"
	secsingame.Parent = leaders
	
	while wait(1) do
		player.leaderstats.Playtime.Value = player.leaderstats.PlayPoints.Value + 1
	end
	
	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync("playpoints"..player.UserId)
	end)
	
	if success then
		secsingame.Value = data
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		ds:SetAsync("playpoints"..player.UserId, player.leaderstats.PlayPoints.Value)
	end)
end)

Thanks for any help!

enable studio api or try to play the game in roblox client not roblox studio.

1 Like

a

what is this though? while wait() stops the code below it, add a task.defer(function() above it too!

task.defer(function()
while task.wait(1) do
		player.leaderstats.Playtime.Value = player.leaderstats.PlayPoints.Value + 1
	end
1 Like

I don’t acutally know what I did before to cause this, but the points just aren’t going up. It started before I put in your function (So it wasn’t your function that caused it), but it worked when I first made them.


(Nvm I found the issue)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.