Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how do i save a players stats on exit?

what i'm trying to do is to save the players stats when they leave and load them back up when they join again

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("DeathSaveSystem")
local ds2 = datastore:GetDataStore("BrickSaveSystem")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local bricks = Instance.new("IntValue")
	bricks.Name = "Bricks"
	bricks.Parent = leaderstats
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function()
			deaths.Value = deaths.Value + 1
		end)
	end)
	
	bricks.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, bricks.Value)
	
	deaths.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, deaths.Value)
	
	game.Players.PlayerRemoving:Connect(function()
		ds2:SetAsync(player.UserId, bricks.Value)
		
	game.Players.PlayerRemoving:Connect(function()
		ds1:SetAsync(player.UserId, deaths.Value)
		end)
	end)
end)

1 answer

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

    local bricks = Instance.new("IntValue")
    bricks.Name = "Bricks"
    bricks.Parent = leaderstats

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Parent = leaderstats

	local id = "player-"..player.UserId
	
		local allData = DataStore:GetAsync(id)
		
		if allData then
			
			bricks.Value = leaderstats.bricks.Value
			deaths.Value = leaderstats.deaths.Value
	
		else
	
			bricks.Value = 0
			deaths.Value = 0
	
		local newplayer = { bricks.Value;
						deaths.Value;
						}
	
		DataStore:SetAsync(id, newplayer)

		end

		player.CharacterAdded:Connect(function(char)
		        local humanoid = char:FindFirstChild("Humanoid")
		        humanoid.Died:Connect(function()
		            deaths.Value = deaths.Value + 1
		        end)
		    end)


game.Players.PlayerRemoving:connect(function(player)
	local id = "player-"..player.userId
	local valor = {leaderstats.bricks.Value;
					leaderstats.deaths.Value;
	                      }
	DataStore:SetAsync(id, valor)
end)

1 comment

0

(Not tested, i just write the code, idk if work)

This discussion is preserved as it was. New answers, comments, and votes are closed.