How to save string value in datastore and display in leaderboard

Hello, I would like to know how I can save a string value called ‘rank’ in the datastore (Make it unique to each player who join the game) and show it on my leader boar… I’ve tried everything but I don’t understand how to do it, if anyone could really help me, I’d really appreciate it. Thank you anyway and happy 2021

I would really appreciate it if someone would explain or help me with this, in advance excuse the inconvenience…

1 Like
game:GetService("Players").PlayerAdded:Connect(function(player)

local store = game:GetService("DataStoreService"):GetDataStore("DataStore")
	
	local leaderstats = Instance.new("folder",player)
	leaderstats.Name = "leaderstats"
	local rank = Instance.new("StringValue",leaderstats)
	rank.Name = "Rank"
	
	local data
	local success, err = pcall(function()
		data = store:SetAsync(player.UserId, 50)
	end)
     
	if success then
		if data then
			rank.Value = data
		else
			-- code for giving them a name here, remember to set rank.Value to there name
		end
	else
		warn(err)
	end
end)



game:GetService("Players").PlayerRemoving:Connect(function(player)

local store = game:GetService("DataStoreService"):GetDataStore("DataStore")

	local success, err = pcall(function()
		store:SetAsync(player.UserId, player.leaderstats:FindFirstChild("Rank").Value)
	end)
     
	if success then
		print("Success!")
	else
		warn(err)
	end
end)

this should work, you would just add a way to give them a rank, though when you say leaderboard I’m not sure if you mean leaderstats leaderboard or an actual object with surface gui and the ranks of players

in the future can you please give us the code of your best attempt so we can se whats wrong

3 Likes

Thank you so much! And this would be a leader board. Thank you very much for real. happy 2021

1 Like