A question about leaderboard

i have a code that i writed, soo idea is very cringe, but im need it, i cant test it cuz i have no friends:(
so idea of code that if player leaves then leaderboard just saves to server, and if he gonna join again then leaderboard gonna load, i have a question, does this work? i didnt even know…
code:

game.Players.PlayerAdded:Connect(function(Player)
if not game.ReplicatedStorage:FindFirstChild(Player.Name) then
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
else
	local ls = game.ReplicatedStorage:FindFirstChild(Player.Name):Clone()
	ls.Parent = Player
	ls.Name = "leaderstats"
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ls = Player:FindFirstChild("leaderstats"):Clone()
ls.Parent = game.ReplicatedStorage
ls.Name = Player.Name
end)
1 Like

Yes it would work if the player rejoins the same server. If somehow a ton of people join this server at different times when it’s still running it might crash though. On a serious note if you’re looking to save data without it being gone the second the server shuts down you should learn datastores.

i tried to do it with this, but… im just a lil braindead i think

its easy really, you just gotta master tables a bit.

1 Like

i have a new code, does this work?

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Cash")
local DataStore1 = DataStoreService:GetDataStore("Server")
game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = Leaderstats
		local Data = DataStore:GetAsync(Player.UserId)
		local Data1 = DataStore1:GetAsync(Player.UserId)
		if Data and game.VIPServerId == Data1 then
			Cash.Value = Data
		end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
DataStore1:SetAsync(Player.UserId, game.VIPServerId)
end)