CharacterAdded without PlayerAdded

Hello! I recently looked into Character added and Player added and I was trying to make a script for only Character added.

Something like this:

game.Players.CharacterAdded:Connect(function()

end)

But that didn’t work, is there a way to do that without game.Players.PlayerAdded?

1 Like

Disclaimer: I am tired. I might not be thinking straight.

To get a specific character you need the Player as well as shown in this article: Player | Roblox Creator Documentation
To get the character without PlayerAdded, you would need a local script. In local scripts you can specifically get he player with local player = game.players.localplayer and once you have that reference to the player you can use character added. local character = player.CharacterAdded...

1 Like

You can’t, you need a player instance to be able to so, Players is a service. There are few ways to do get a palyer instance immediately

  • Localscripts allow the use of the property LocalPlayer, which returns the player related to that client
  • IF you have the name of the player, you can find the first child i nthe service with their name
  • If you have their character from the workspace, you can use the player function GetPlayerFromCharacter to get their player instance

It all depends on what you want to do

1 Like

To clear up some confusion on what I want, I want so every time a character respawns it applies a function on him.

1 Like

Nope, I don’t think that there is a function that fires each time a character has been added without a player.

You can use a ChildAdded function and check if it’s a player’s character with GetPlayerFromCharacter but it’s easier to use a PlayerAdded & CharacterAdded function together instead.

1 Like

Do you want it local or server sided? If you want it server sided, you hae to use PlayerAdded to get the player instance and then CharacterAdded, but if it’s local, you can use game.Players.LocalPlayer to get the player instance and connect CharacterAdded to that

You can add a script inside the PlayerGui or the Character and It will get fired each time that the Player respawns, but PlayerAdded with CharacterAdded works the same way.

1 Like

If your using a localscript to get a player each time added you can insert a localscript inside StarterCharacterScripts which will be added into the player’s character or simply use

player.Character or player.CharacterAdded:Wait()

Thank you for help I’ll try to figure something out with this info.

Are you trying to fire the function in a Server Script or in a Local Script? Where do you plan on placing the script?

1 Like

With this post I’d like to briefly cover three most common scenarios and explain them.


1. Server script

In case we are waiting for character to be added on the server, need to wait for it to be added into workspace.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		-- further code
	end)	
end)

Each time player respawns, they are assigned a new character. The old character is destroyed and cleared from memory unless it’s strongly referenced. Player manages the new character that appears in workspace. CharacterAdded fires each time a character model is assigned to certain player instance.

But doesn’t leaving connections like this potentially cause memory leaks? No, not in this case, since all those connections are eventually disconnected and cleared once player leaves the game.

Open this fold to see what not to do and why.
game:GetService("Players").PlayerAdded:Connect(function(player)
	-- Find existing character or wait for it to be added.
	local character = player.Character or player.CharacterAdded:Wait()
	-- further code
end)

--[[
	Why won't this work well? It will first time player joins, but
	nothing will happen after player respawns.
	
	"character" variable only stores the last character, even when it
	is removed. Any changes done to it won't be visible on new character
	nor its descendants.
]]

Others have already explained this.

2. Local script - StarterCharacterScripts

All scripts that are located in StarterCharacterScripts are inserted into player’s character model. That means each time new character spawns, all these scripts will activate again. Not only is .CharacterAdded event unnecessary here, but it burdens memory for no reason.

DO NOT CONNECT .CharacterAdded in LOCAL SCRIPTS! (Sorry, had to stress it out. :smile:)

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

Voila! Simple, right? You should use StarterCharacterScripts for any scripts that should be part of newly parented character.

3. Local script - StarterPlayerScripts

What is the catch here? Local scripts located here are not inserted into any characters, but rather remain in player instance. They are not freshly inserted after player respawns, and stay on spot until player leaves os player instance changes. The process is similar to server script scenario, although you shouldn’t use StarterPlayerScripts to do changes on active character. If you are managing camera, for example, StarterPlayerScripts are a good choice.

1 Like

There’s no way but you can use a folder in the workspace and put all the players’ characters there, then check if a new character is added with ChildAdded.

workspace.Characters.ChildAdded:Connect(function(Character)

end)
1 Like

characteradded is only for a player
like
the player gets added to game.players
and his character gets added to workspace
the only thing where you can use characteradded is:

game.Players.PlayerAdded:Connect(function(player)
     player.CharacterAdded:Connect(function(char)
end)
end)