Hey i made this script > `local DataService = game:GetService(“DataStoreService”)
local LevelData = DataService:GetDataStore(“Levels”)
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new(“Folder”, plr)
leaderstats.Name = “leaderstats”
local Level = Instance.new(“IntValue”, leaderstats)
Level.Name = “Levels”
for i, v in pairs(game.Workspace:GetChildren()) do
if v.Name == plr.Name then
local LevelGui = game.ReplicatedStorage.BillboardGui:Clone()
LevelGui.Parent = v
end
end
local data
local success, errorMessage = pcall(function()
data = LevelData:GetAsync(plr.UserId)
end)
if success then
Level.Value = data
LevelGui.TextLabel.Text = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errorMessage = pcall(function()
LevelData:SetAsync(plr.UserId, plr.leaderstats.Levels.Value)
end)
end)
But i can’t find out how to get the character from the playeradded.
local DataService = game:GetService("DataStoreService")
local LevelData = DataService:GetDataStore("Levels")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local Level = Instance.new("IntValue", leaderstats)
Level.Name = "Levels"
local LevelGui = game.ReplicatedStorage.BillboardGui:Clone()
LevelGui.Parent = plr.Character.Head
local data
local success, errorMessage = pcall(function()
data = LevelData:GetAsync(plr.UserId)
end)
if success then
Level.Value = data
LevelGui.TextLabel.Text = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errorMessage = pcall(function()
LevelData:SetAsync(plr.UserId, plr.leaderstats.Levels.Value)
end)
end)
If that doesn’t work, you may need to use “plr.CharacterAdded:Connect(function(character)”
This might because the player’s character hasn’t loaded in yet. To not cause an error, we need to listen for the event (in this case “plr.CharacterAdded:Connect(function(character)”.) that triggers when the player’s character gets added into the game.
Indentation in Lua isn’t required, it’s just a stylistic choice which allows for more readable code.
local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local BBGui = Replicated:WaitForChild("BillboardGui")
Players.PlayerAdded:Connect(function(Player)
local Data
Player.CharacterAdded:Connect(function(Character)
local BBGuiClone = BBGui:Clone()
BBGuiClone.Parent = Character
BBGuiClone.TextLabel.Text = Data or ""
end)
local LS = Instance.new("Folder")
LS.Name = "leaderstats"
LS.Parent = Player
local Level = Instance.new("IntValue")
Level.Name = "Levels"
Level.Parent = LS
local Succ, Err = pcall(function()
Data = LevelData:GetAsync(Player.UserId)
end)
if Succ then
Level.Value = Data
else
warn(Err)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Data
local Succ, Err = pcall(function()
Data = LevelData:SetAsync(Player.UserId, Player.leaderstats.Levels.Value)
end)
if Succ then
return
else
warn(Err)
end
end)