Trying to place all characters in one Model

I’m having issues with this script and it’s not registering for somewhat reason.

Upon the character’s spawn, I am trying to place them in a Model within the Workspace called Client_Directory (along with other players).

local Players = game:GetService("Players")

local clientdirectory = Instance.new("Model")
clientdirectory.Name = "Client_Directory"
clientdirectory:Clone().Parent = workspace

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local PlayerCheck = Players:GetPlayerFromCharacter(character)
		local Model = game.Workspace:FindFirstChildWhichIsA("Model")
		local ModelName = Model.Name

		if ModelName == PlayerCheck.Name then
			Model.Parent = game.Workspace.Client_Directory
		end
	end)	
end)

This is a serverscript placed in the ServerScriptStorage but it is not working. What am I doing wrong?

1 Like

My first problem I see with your code is that you are grabbing the first model in the workspace and checking to see if it’s a player’s character. You are given the character in the character added event and you don’t need to do a player check since this is 100% going to be a players character.

My second problem I see with this is that it’ll only start running after the second time a character loads in. player.CharacterAdded doesn’t seem to run when a player first loads in but runs every time after so my solution for that is making the player.CharacterAdded call a function and then calling it the second a player joins. May seem a bit confusing but I’ll give you an example for what your code should look like:

local Players = game:GetService("Players")

local clientdirectory = Instance.new("Model")
clientdirectory.Name = "Client_Directory"
clientdirectory:Clone().Parent = workspace

local function characterAdded(character)
character.Parent = clientdirectory
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(characterAdded) -- this will automatically pass the character parameter so there's no need to add it in
    characterAdded(player.Character or player.CharacterAdded:Wait()) -- this here is so the characterAdded event will run the first time they join 
end)
2 Likes

There are several issues with the script that cause the weird error:

  1. local clientdirectory = Instance.new("Model") clientdirectory.Name = "Client_Directory" clientdirectory:Clone().Parent = workspace
    Here you are creating a model, and once the model is created, it’s parent is set to NIL.
clientdirectory:Clone().Parent = workspace

This line is cloning the model to workspace
However, this is cloning it not declaring the initial value, meaning that now you have 2 model is the game:

  1. clientdirectory model which parent is NIL
  2. clientdirectory model which is put inside workspace

And even if your code is working, the

local clientdirectory = Instance.new("Model")  ~=   clientdirectory:Clone().Parent = workspace

local PlayerCheck = Players:GetPlayerFromCharacter(character)

local Model = game.Workspace:FindFirstChildWhichIsA("Model")

These lines are meaningless

At first here you are detecting when player is joined into the game, which is correct and which means we already have the player instance

game.Players.PlayerAdded:Connect(function(player) -- "player"

Also for the Character

player.CharacterAdded:Connect(function(character) -- character

since that’s a player and the function “CharacterAdded” returns “character” model

		local ModelName = Model.Name

		if ModelName == PlayerCheck.Name then
			Model.Parent = game.Workspace.Client_Directory
		end

Here these lines are checking the the character 's name is the player’s name which we don’t need.
Also if there another model in the workspace it would cause an issue.


Here is the fixed script:

local Players = game:GetService("Players")

local clientdirectory = Instance.new("Model")
clientdirectory.Name = "Client_Directory"

clientdirectory.Parent = game.Workspace

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		task.wait() -- a little wait before the character is completelly inside workspace
		
		character.Parent = clientdirectory
	end)	
end)

If I have mistake, you don’t understand something or I have mistake, go ahead and feel free to reply! :grin:

1 Like

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