Player Added func not working or game not getting the LocalPlayer

Hello.today i was testing something and did a really easy script:

local Players = game:GetService(“Players”)
local CurrentCam = workspace.CurrentCamera
local Camera = workspace.Camera
local LocalPlayer = Players.LocalPlayer
local function onPlayerAdded(Player)
if LocalPlayer then
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded
local Humanoid = Character:WaitForChild(“Humanoid”)
if Humanoid then
print(“Hum Found”)
end
end
end

if LocalPlayer then
print(“Yes”)
onPlayerAdded()
end

The function should be fired if there is a LocalPlayer,but it isn’t working. i tried to print words to make sure the script gets the LocalPlayer, but it doesn’t print. I used the Player Added func too but it doesn’t work either. i readed a few posts with people with my same problem but i still couldn’t find an answer/fix. The script is just a LocalScript in StarterPlayerScripts.
thanks in advance for any help/answers.

3 Likes

LocalPlayer always exists in local scripts and can never be nil

Try changing this to local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

3 Likes

A bit strange in a LocalScript but sure.


Why would you have a Player parameter if you already had the LocalPlayer?


You’re checking if the LocalPlayer exists twice.

1 Like

This little snippet works.

Are you saying that LocalPlayer is a global in LocalScripts? Cause I could guarantee it’s not.

1 Like

I mean game.Players.LocalPlayer (which is defined as LocalPlayer in the script), I might need to rephrase that

2 Likes

Ah it’s alright now I know what you mean.

2 Likes

If this is a localscript, the LocalPlayer should always exist because Player objects cant randomly be destroyed unless player leaves

local Players = game:GetService(“Players”)
local Camera = workspace.CurrentCamera
local LocalPlayer = Players.LocalPlayer

local Character = LocalPlayer.Character or LocalPlayerCharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

print(Humanoid:GetFullName())
3 Likes

yeah i will remove the second check inside the function,thanks :slight_smile:

2 Likes

alright i will try this as soon as i can, thank you :slight_smile:

2 Likes

Try this one;

local Players = game:GetService("Players")
local Camera = workspace.Camera
local CurrentCamera = workspace.CurrentCamera

local Client = Players.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")

if Humanoid then
    print("Found humanoid in client's character")
end

There’s no need to use the PlayerAdded event since the LocalPlayer will always exist in the game.Players data model. Although there’s no guarantee that it is fully loaded, it will always be present.

In this scenario, you can employ custom workarounds to detect if a player has been fully loaded, such as using Client:WaitForChild(“PlayerGui”). However, please avoid attempting to use this solution from the local client to index PlayerGui from other players as it is not replicated between clients.

1 Like

alright i tested this way and it worked. thank you guys so much.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.