Help me to do this with DataStore

I want to save the UserName of the players who join with the DataStore. Then put that information into a TextLabel and Clone into a GUI. And every time I open it, I see what I’ve saved.

I can get the UserName of those who joined. But struggled with saving it with DataStore (though tried many times). Someone please help me, really confused with DataStore. Most tutorials about DataStore are about saving currency. So I don’t know how to use it in many cases.

1 Like

I recommend you check out the Documentation tutorial for it, because it explains really well what data store is, why and how

local DS = game:GetService("DataStoreService"):GetDataStore("JoinLogs")

game:GetService("Players").PlayerAdded:Connect(function(curPlr)

	local key = "i4_" .. curPlr.Name

	local success, err = pcall(function()
		return DS:SetAsync(key, {curPlr.Name, curPlr.DisplayName, curPlr.UserId, os.date("%x, %X")})
	end)

	if success then
		print("Saved " .. curPlr.Name)
	else
		print("Failed to save")
		warn(err)
	end
end)

I have succeeded in saving the information (like the above script). But I don’t know how to get the data out (have read and tried the doc you gave). Can you give an example to get the data out according to the above script?

The same link also explained that:

Basically use :GetAsync to get data and :SetAsync is supposed to be called when you want to save data to a specific key and is usually used when the player leaves using .PlayerRemoving, GetAsync has a single parameter which is the key name, it will return the value of the key in their datastore servers.

Please read the tutorial again thoroughly and understandably so you can hopefully understand everything about datastores.

If you need to see my code: https://pastebin.com/jvMhP076
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local RunService = game:GetService(“RunService”)
local Player = game.Players.LocalPlayer
local DataStoreService = game:GetService(“DataStoreService”)
local dataStore = DataStoreService:GetDataStore(“players”)
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local MainGui = PlayerGui:WaitForChild(“MainGui”)

local Players = game:GetService(“Players”)

local function addData(player)
local dataPromise = dataStore:SetAsync(player, player.Name)
local success, data = pcall(function()
return dataPromise:Await(5)
end)

local playerData = Instance.new("TextLabel")
playerData.Name = "PlayerData"
playerData.Text = player.Name
playerData.Parent = MainGui
playerData.BackgroundTransparency = 1
playerData.Size = UDim2.new(0.1, 0, 0.1, 0)
playerData.Position = UDim2.new(0, 0, 1, 0)
playerData.TextColor3 = Color3.new(1, 1, 1)

end

Players.PlayerAdded:Connect(addData)