How to get the character from a playeradded event?

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.

You can just use “plr.Character” or “plr.CharacterAdded:Connect(function(character)”.

the “plr.Character” Becomes nil

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)

Hmm. Try aligning the block of code so that it matches up with the text that says “local leaderstats”.

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.

1 Like

Nah nvm i found out, i kinda knew it from the start but i didn’t check. It was beacuse i didn’t give time for the character to load

1 Like

I am glad that your issue has been resolved! Have a great day.

1 Like

Thank you, have a nice day you to.

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)

Thanks for the info! I always thought that alignment was important for the code to run properly.