PLS HELP data wont store

i opened a previous thread about this script ive been messing with and for some reason the data will not save or store itself after a player leaves the game its frustrating me cuz i cant seem to figure out if its this script or another one interferring with it

--
 game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
    Leaderstats.Parent = Player
    local Minutes = Instance.new("IntValue")
    Minutes.Name = "Time"
    Minutes.Value = 0
    Minutes.Parent = Leaderstats
	
	
    local Data = TimeStore:GetAsync(Player.UserId)
    if Data then
        Minutes.Value = Data
    end
    
    coroutine.resume(coroutine.create(function()
        while true do
            wait(60)
            Minutes.Value = Minutes.Value + 1
        end
    end))
end)

game.Players.PlayerRemoving:Connect(function(Player)
	TimeStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)

game:BindToClose(function()
	for i, Player in pairs(game.Players:GetPlayers()) do
		TimeStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
	end
end)
2 Likes

is there any error in the console ?

none at all i cant seem to find any obvious flaw

can you add a print inside Players.PlayerRemoving
then send me a photo of the console

Minutes is a variable, not a descendant of Leaderstats.

but why is there no error, normally it would say Cannot find minutes inside leaderstats

I’ll help you.

Replace with this:

while player.Parent do
Minutes.Value += (math.floor(task.wait(60))/60)
TimeStore:SetAsync(player.UserId, Minutes.Value)
end

Your data will autosave if the server goes down.

Try this

--
--
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Minutes = Instance.new("IntValue")
	Minutes.Name = "Time"
	Minutes.Value = 0
	Minutes.Parent = Leaderstats


	local Data = TimeStore:GetAsync(Player.UserId)
	if Data then
		Minutes.Value = Data
	end

	coroutine.resume(coroutine.create(function()
		while true do
			task.wait(60)
			Minutes.Value += 1
		end
	end))
end)

Players.PlayerRemoving:Connect(function(Player)
	TimeStore:SetAsync(Player.UserId, Player.leaderstats.Time.Value)
end)

game:BindToClose(function()
	for i, Player in pairs(Players:GetPlayers()) do
		TimeStore:SetAsync(Player.UserId, Player.leaderstats.Time.Value)
	end
end)
2 Likes

never use this, this will lag datastore

What is TimeStore defined as?

Use task.defer instead of coroutines

After every 60 seconds? I doubt it. The DataStoreService has been getting more and more optimized. Autosaving every 60 seconds is a bad practice in large servers.

Take a look at this video - >

Even though it’s for an overhead system. I did include a Leaderstats script that is a little more modified and could help you fix this problem!

Coroutines and task.defer aren’t necessary in the script he posted, which is why I removed them.

i tried replacing Player.leaderstats.Minutes.Value with Player.leaderstats.Time.Value it didnt work let me try some of the other solutions listed

This solution fixes the spelling mistake from the OP’s code, but the coroutine should also be preferably removed. What is it even for, anyways? The loop is at the end of the thread.

1 Like

What do you have TimeStore defined as?

I believe he defined it as the datastore.

can you send a photo of where the script is located

TwinPlayz really said plug‎‎‎‎‎‎‎‎

1 Like