Always having issues with Datastore

Here is DataStore Example. Note: edit the name of the DataStore and other things.
Code:

local DataStoreService = game:GetService("DataStoreService")
    local MinutesPlayedDataStore = DataStoreService:GetDataStore("MinutesPlayed")

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

    	local MinutesPlayedInt = Instance.new("IntValue")
    	MinutesPlayedInt.Name = "Minutes Played"
    	MinutesPlayedInt.Parent = leaderstats

    	local data
    	local success, errormessage = pcall(function()
    		data = MinutesPlayedDataStore:GetAsync(player.UserId.."-minutes played")
    	end)

    	if success then
    		MinutesPlayedInt.Value = data
    	else
    		print("There was an error while getting "..player.Name.." data!")
    		warn(errormessage)
    	end
    end)

    game.Players.PlayerRemoving:Connect(function(player)
    	local success, errormessage = pcall(function()
    		MinutesPlayedDataStore:SetAsync(player.UserId.."-minutes played", player.leaderstats["Minutes Played"].Value)
    	end)

    	if success then
    		print(player.Name.." data saved!")
    	else
    		print("There was an error while saving "..player.Name.." data!")
    		warn(errormessage)
    	end
    end)
1 Like