Character Added only firing for the first player

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create an admin/controller script
  2. What is the issue? Include screenshots / videos if possible!
    The PlayerAdded/CharacterAdded event is only firing when the first player joins
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have already checked the devforum for solutions but I couldn’t find anything :frowning:

The Script:

local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local Commands = require(script.Commands)

local NoiceCore = SS.NoiceCore

local adminUI = SS.NoiceCore.Admin
local values = script.Values

coroutine.wrap(function()
	local function characterFunction(character)
		local hum:Humanoid = character:WaitForChild("Humanoid")
		local player:Player = Players:GetPlayerFromCharacter(character)

		hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
		hum.BreakJointsOnDeath = false

		local newUsernameDisplay = NoiceCore:WaitForChild("UsernameGui"):Clone()
		newUsernameDisplay.Parent = character.PrimaryPart
		newUsernameDisplay.Adornee = character.PrimaryPart
		newUsernameDisplay:WaitForChild("Name").Text = character.Name

		for i, v in pairs(character:GetChildren()) do
			if v:IsA("BasePart") then
				v.CollisionGroup = "Players"
			end
		end

		hum.Died:Connect(function()
			coroutine.wrap(Commands.Ragdoll)(character, 2.5)
			characterFunction(character)
			if values.KickOnDeath.Value == true then
				player:Kick("Kick on death setting is on.")
			end
		end)

		print(character.Name, "Character Added")
		if character.Name == "wilwil_bos" then
			print(character.Name, "Admin Added")
			local adminPlayer = Players:GetPlayerFromCharacter(character)
			local openButton = NoiceCore:WaitForChild("Open"):Clone()
			local newAdminGui = nil
			local newOpenGui = nil
			if not adminPlayer.PlayerGui:FindFirstChild("Admin") then
				local adminClone = adminUI:Clone()
				adminClone.Parent = adminPlayer.PlayerGui
				adminClone.Enabled = false
				newAdminGui = adminClone
			elseif adminPlayer.PlayerGui:FindFirstChild("Admin") then
				adminPlayer.PlayerGui:FindFirstChild("Admin"):Destroy()
				local adminClone = adminUI:Clone()
				adminClone.Parent = adminPlayer.PlayerGui
				adminClone.Enabled = false
				newAdminGui = adminClone
			end
			if not adminPlayer.PlayerGui:WaitForChild("GameGui"):FindFirstChild("Open") then
				local openClone = openButton:Clone()
				openClone.Parent = adminPlayer.PlayerGui.GameGui
				openClone.Visible = true
				newOpenGui = openClone
			elseif adminPlayer.PlayerGui:WaitForChild("GameGui"):FindFirstChild("Open") then
				adminPlayer.PlayerGui:WaitForChild("GameGui"):FindFirstChild("Open"):Destroy()
				local openClone = openButton:Clone()
				openClone.Parent = adminPlayer.PlayerGui:WaitForChild("GameGui")
				openClone.Visible = true
				newOpenGui = openClone
			end
			game.ReplicatedStorage.Events.AdminEvent.OnServerEvent:Connect(function(player, command, info, num, secondaryInfo)
				Commands.AdminFunction(command, info, player, num, secondaryInfo)
			end)
		end
	end
	
	function playerFunction(player)
		if values:WaitForChild("KickOnJoin").Value == true then
			player:Kick("This server has been locked by the owner. Please join a different server.")
		end

		NoiceCore:WaitForChild("AdminEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore:WaitForChild("FlyEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore:WaitForChild("UnflyEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore.HoldingBox.Parent = workspace

		local immortal = Instance.new("BoolValue")
		immortal.Name = "Immortal"
		immortal.Parent = player
		immortal.Value = false

		print(player.Name, "Joined")
		
		for i, v:Tool in pairs(game:GetDescendants()) do
			if v:IsA("Tool") then
				local handle = v:FindFirstChild("Handle")

				if handle then
					local touchInterest = handle:FindFirstChild("TouchInterest")
					if touchInterest then
						touchInterest:Destroy()
					end
				end
			end
		end

		workspace.DescendantAdded:Connect(function(desc)
			if desc:IsA("Tool") then
				local handle = desc:FindFirstChild("Handle")

				if handle then
					local touchInterest = handle:FindFirstChild("TouchInterest")
					if touchInterest then
						touchInterest:Destroy()
					end
				end
			end
		end)

		if player.Character then characterFunction(player.Character) end
		player.CharacterAdded:Connect(characterFunction)
	end
end)()

Players.PlayerAdded:Connect(playerFunction)

for i, v in pairs(Players:GetPlayers()) do
	playerFunction(v)
end

Any help appreciated!

1 Like

For the first player, does characterFunction trigger after that first player dies and respawns?

1 Like

Yes, the “wilwil_bos character added” prints in the output, but nothing else.

Try removing the coroutine.wrap() block, there’s no need to define it as such since there’s no thread yielding or synchronous operations (like wait()).

		NoiceCore:WaitForChild("AdminEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore:WaitForChild("FlyEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore:WaitForChild("UnflyEvent").Parent = game:GetService("ReplicatedStorage").Events
		NoiceCore.HoldingBox.Parent = workspace

Also, you’re re-parenting NoiceCore’s children every time a player joins the game. By the time a second player joins, the children might not exist anymore in NoiceCore since they were already re-parented by the first player. Are you sure you checked the output logs for errors or statements such as “infinite yield possible”?

I removed the waitforchild and it WORKED. I think the waitforchild was clogging up the function. thanks for the help though!

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