How to know if a local player join/leave a game?

  1. What do you want to achieve?
    How to know if the local player join/leave a game ?
  2. What is the issue?
    I don’t know if the local player join/leave a game.
  3. What solutions have you tried so far?
    I’ve tried this but that doesn’t work (when the player die that print leave) :
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Connect(function()
	print("Join")
end)

Player.CharacterRemoving:Connect(function()
	print("Leave")
end)
3 Likes

That’s because you used CharacterAdded and CharacterRemoving instead of PlayerAdded and PlayerRemoving, the character gets removed and added back on respawn/death

hope this helped!

1 Like
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    print(Player.Name, " Joined!")
end)

Players.PlayerRemoving:Connect(function(Player)
    print(Player.Name, " Left!")
end)
2 Likes

I’ve do this but that doesn’t print “join” (Local script) ! :neutral_face:

local Players = game.Players
local Player = game.Players.LocalPlayer

Players.PlayerAdded:Connect(function()
	Player.CharacterAdded:Connect(function()
		print("Join")
	end)
end)

Players.PlayerRemoving:Connect(function()
	Player.CharacterRemoving:Connect(function()
		print("Leave")
	end)
end)

I know this way but I’m searching how to know when the local player join/leave and that said me only when a player not local leave or join a game ! :grinning:

Serverscripts can’t use localplayer, instead add a “player” argument on the player join event

Example:

Players.PlayerAdded:Connect(function(player)

(and the leave event too)

It should look like this:

local Players = game.Players

Players.PlayerAdded:Connect(function(player)
	Player.CharacterAdded:Connect(function()
		print("Join")
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	Player.CharacterRemoving:Connect(function()
		print("Leave")
	end)
end)
1 Like

I’m in a local script ! :grinning: (5454854)

Then why are you making a player join/leave script when its happening locally?

1 Like

You could try yielding the script until it finds the player, though I do not see why your script does not work.

if not game.Players.LocalPlayer then game.Players.PlayerAdded:Wait() end
1 Like

The thing is You cannot check does local player join / leave.
When player joins all the local scripts begin to function and when the local player leave, all the local scripts get deleted.
As because all of the processes take place on client-side then it’s basically impossible.

3 Likes

Thank you very much ! :grin:
So I can’t do that ! :sweat_smile:

Your client is your player, so when the Local Script plays, your player should already be in-game.

1 Like

If local scripts exists then local player is in-game.
Obviously if its not then local player is not ingame.

1 Like

Thats how the local scripts work

1 Like

I’ve do this and it’s work ! :grin:

local Players = game.Players
local Player = game.Players.LocalPlayer

print("join")

Players.PlayerRemoving:Connect(function()
	Player.CharacterRemoving:Connect(function()
		print("Leave")
	end)
end)

That doesnt make sense tho.
You can bind the line where You print ‘leave’ just like this.

game.Players.LocalPlayer.Destroying:Connect(function()
       -- ur code here
end) 

Local script n character get destroyed last therefore You can optimize Your script into 3 lines.

1 Like

Also - CharacterAdded and CharacterRemoving launches everytime player respawn. Thats because character get removed and added back once again but alive (after character death)

1 Like

Your script doesn’t work (I’ve tested-it) … :neutral_face:

Then that means that Roblox changed stuff in Luau.
Don’t really know why It doesn’t work tho.

1 Like

You can do:

game.Players.PlayerRemoving:Connect(function(plr)
	if plr == game.Players.LocalPlayer then
         -- function below here
		print("Aa")
	end
end)

I did try it, Hope it helps!

1 Like