Datastore refuses to save

im currently implementing time into my game using data stores and so forth but it wont save on a players disconnect ive used both bindtoclose and playerremoving but i just cant get it to work currently i have this

-- 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:BindToClose(
	game.Players.PlayerRemoving:Connect(function(Player)
    TimeStore:SetAsync(Player.UserId, Player.Leaderstats.Minutes.Value)
	end)
	)

Seperate the saving functions. This way it will work :slight_smile:

The problem is that you are trying to use a PlayerRemoving function inside of a game:BindToClose function.

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)
3 Likes

ive done this and it still refuses to save

~~lua
local DataStoreService = game:GetService(“DataStoreService”)
local TimeStore = DataStoreService:GetDataStore(“TimeStats”)

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)
~~
could it be because my datastore isnt set up right im new to lua so im sorry

It’s because you capitalized leaderstats, which I didn’t realize.

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)

it still refuses to work n im bewildered why