DataStore not working!

hey yall
So I was trying a datastore I made until I ran into a problem.
When I add 10 Studs it goes back to 0 and (maybe) does not save!
I don’t know what to do so now I am here.

Script
--- Add Leaderstats ---
game:GetService("Players").PlayerAdded:Connect(function(Player)
	local Folder = Instance.new("Folder", Player)
	Folder.Name = "leaderstats"
	local NumberValue = Instance.new("NumberValue", Folder)
	NumberValue.Name = "Studs"
end)

--- Add Studs/minute and Save Currency ---
game:GetService("Players").PlayerAdded:Connect(function(Player)
	local NumberValue = Player:WaitForChild("leaderstats").Studs
	local DataStore = game:GetService("DataStoreService")
	local GameDataStore = DataStore:GetDataStore("GameDataStore")
	local Data

	if Player.MembershipType == Enum.MembershipType.None then
		while wait(60) do
			Player.leaderstats.Studs.Value += 5
			wait(1)
			local Success, ErrorMessage = pcall(function()
				Data = GameDataStore:GetAsync(Player.UserId.."-NumberValue")
			end)
			
			if Success then
				NumberValue.Value = Data
			else
				local GUI = Player:WaitForChild("PlayerGui").DataSaveFailed
				GUI.TextLabel.Visible = true
				warn("Saving for player ||"..Player.Name.."|| failed!")
			end
		end
	elseif Player.MembershipType == Enum.MembershipType.Premium then
		while wait(30) do
			Player.leaderstats.Studs.Value += 10
			wait(1)
			local Success, ErrorMessage = pcall(function()
				Data = GameDataStore:GetAsync(Player.UserId.."-NumberValue")
			end)

			if Success then
				NumberValue.Value = Data
			else
				local GUI = Player:WaitForChild("PlayerGui").DataSaveFailed
				GUI.TextLabel.Visible = true
				warn("Saving for player ||"..Player.Name.."|| failed!")
			end
		end
	end
end)

--- Save Currency When Player Is Leaving ---
game:GetService("Players").PlayerRemoving:Connect(function(Player)
	local NumberValue = Player.leaderstats.Studs
	local DataStore = game:GetService("DataStoreService")
	local GameDataStore = DataStore:GetDataStore("GameDataStore")
	
	local Success, ErrorMessage = pcall(function()
		GameDataStore:SetAsync(Player.UserId.."-NumberValue", Player.leaderstats.Studs.Value)
	end)
	
	if Success then
		print("Data for player ||"..Player.Name.."|| was saved succesfully!")
	else
		warn("Saving data upon leaving for player ||"..Player.Name.."|| failed!")
	end
end)

Thank you! Waiting for a reply.
Attempt 2

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local GameDataStore = DataStore:GetDataStore("GameDataStore")

local DefaultValue = 0
local retries = 3

local function DataMethod(Method, Key, Data)
    local success, ret
    local count = 0
    repeat
        success, ret = pcall(GameDataStore[Method], GameDataStore, Key, Data )
    
        count += 1
    until success or count >= retries
    
    return success, ret
end

function PlayerJoined(Player)
    local Leaderstats = Instance.new("Folder", Player)
    Leaderstats.Name = "leaderstats"
    local Studs = Instance.new("NumberValue", Folder)
    Studs.Name = "Studs"
    
    local _, Data = DataMethod("GetAsync", Player.UserId.."-NumberValue" )
    Studs.Value = Data or DefaultValue
    
    if Player.MembershipType == Enum.MembershipType.None then
       
        while task.wait(60) do
            Studs.Value += 5
        end
        
    elseif Player.MembershipType == Enum.MembershipType.Premium then
        
        while task.wait(30) do
            Studs.Value += 10
        end
    end
end

function Save(Player)
    local Studs = Player.leaderstats.Studs
    
    local Success = DataMethod("SetAsync", Player.UserId.."-NumberValue", Studs.Value)
    
    if Success then
      print("Data for player ||"..Player.Name.."|| was saved succesfully!")
    else
        warn("Saving data upon leaving for player ||"..Player.Name.."|| failed!")
    end
end

for _, Player in ipairs(Players:GetPlayers()) do
    task.spawn(PlayerJoined, Player)
end
Players.PlayerAdded:Connect(PlayerJoined)

Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
    for _, Player in ipairs(Players:GetPlayers()) do
        task.spawn(Save, Player)
    end
    task.wait(3)
end)
1 Like

OH WOW! TYSM!!! (character cap is bad)

1 Like