CharacterAdded not going through

Hey! So I made a script that is for voting system and it starts rounds and teleports people to the map to play. Everything works except when you reset, it doesn’t send you back to the lobby? I have no idea why characteradded is not going through.

Entire Script
local rep = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local votingSystem = game.Workspace.Voting
local status = rep.Status

local redTeam = game.Teams["Killer"]
local blueTeam = game.Teams["Runner"]
local blueTeamColor = BrickColor.new ("Lime green")
local redTeamColor = BrickColor.new ("Really red")

local lobbyTeam = game.Teams.Lobby

local choices = votingSystem:GetChildren()
local maps = rep.Maps:GetChildren()

local intermission = 10

local isAnOption
local randomMap

local chosenMap
local mapClone

local BlueTeamCount
local RedTeamCount


-- picking a random map function
local function PickRandomMap ()

	local randomNumber = math.random(1, #maps)

	randomMap = maps[randomNumber]

	return randomMap.CanBeVoted
end


for i, choice in pairs(choices) do

	local name = choice.label.SurfaceGui.TextLabel
	local picture = choice.Image.SurfaceGui.ImageLabel

	isAnOption = PickRandomMap()

	if isAnOption.Value == true then
		repeat 
			isAnOption = PickRandomMap()
		until
		isAnOption.Value == false
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true

	else
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true		
	end					
end	


rep.InRound.Changed:Connect(function()

	if rep.InRound.Value == false then

		mapClone:Destroy()

		for i, map in pairs(maps) do
			map.CanBeVoted.Value = false
		end

		for i, choice in pairs(choices) do

			local name = choice.label.SurfaceGui.TextLabel
			local picture = choice.Image.SurfaceGui.ImageLabel

			isAnOption = PickRandomMap()

			if isAnOption.Value == true then
				repeat 
					isAnOption = PickRandomMap()
				until
				isAnOption.Value == false
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true

			else
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true		
			end					
		end	


	else

		-- after the intermission has ended, the round will soon begin
		--- when the map with most votes will be spawned

		local Choice1Votes = #votingSystem.Choice1.button.Votes:GetChildren()
		local Choice2Votes = #votingSystem.Choice2.button.Votes:GetChildren()
		local Choice3Votes = #votingSystem.Choice3.button.Votes:GetChildren()

		if Choice1Votes >= Choice2Votes and Choice1Votes >= Choice3Votes then

			chosenMap = votingSystem.Choice1.label.SurfaceGui.TextLabel.Text

		elseif Choice2Votes >= Choice1Votes and Choice2Votes >= Choice3Votes then

			chosenMap = votingSystem.Choice2.label.SurfaceGui.TextLabel.Text

		else

			chosenMap = votingSystem.Choice3.label.SurfaceGui.TextLabel.Text

		end

		-- shows which map has won

		status.Value = "The Chosen map is: ".. chosenMap

		--- getting the map from the replicated storgae to the workspace

		for i, map in pairs(maps) do
			if chosenMap == map.Name then
				mapClone = map:Clone()
				mapClone.Parent = game.Workspace
			end
		end	

		--[[ another short delay right before the players get teleported you would also need to add 
		the same delay on the round system script]]
		wait(1.5)

		-- clears all the votes for next round

		for i, choice in pairs(choices) do
			choice.label.SurfaceGui.TextLabel.Text = " "
			choice.Image.SurfaceGui.ImageLabel.Image = " "
			choice.button.Votes:ClearAllChildren()
			rep.VoteReset:FireAllClients(choice.button)
		end

		-- this is if there are no teams wit dedicated spawns (free for all):

		--local spawns = mapClone.Spawns:GetChildren()
------------------
            ------
            -----
            -- Test hehehe
            -----------
		local BlueTeamCount = {}
		local RedTeamCount = {}

		for i, plr in pairs(plrs:GetChildren()) do

			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")
			local nameGui = char.Head:FindFirstChild("NameGUI")

			local RedSpawns = mapClone.RedSpawns:GetChildren()
			local BlueSpawns = mapClone.BlueSpawns:GetChildren()

			-- picks a random spawn from each team
			local randomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
			local randomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]
--------------------------------------------
-------------------------------------------------------- Random Teams Rewrite ------------------------------------------------------
--------------------------------------------
			if #RedTeamCount == 1 then

				plr.Team = blueTeam
				-- this is so we can make the spawn location for each player more randomized
				humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
				plr.CameraMode = "Classic"
				plr.CameraMaxZoomDistance = 9
				table.insert(BlueTeamCount, plr.Name)
				if nameGui then
					nameGui.name.TextColor3 = blueTeamColor
				end
				print(plr.Name .. " put in ".. plr.Team.Name)

			elseif #RedTeamCount == 0 then

				plr.Team = redTeam
				plr.CameraMode = "LockFirstPerson"
				plr.CameraMaxZoomDistance = 0.5
				humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)

				if nameGui then
					nameGui.name.TextColor3 = redTeamColor
				end

				print(plr.Name .. " put in ".. plr.Team.Name)

				-- if both teams have the same amount of players, it choses a random team using math.random

			else

				local u = 0

local function chosen(x,y)
   u = u + 1
   if x ~= nil and y ~= nil then
      return math.floor(x + (math.random(math.randomseed(os.time()+u))*999999 %y))
   else
      return math.floor((math.random(math.randomseed(os.time()+u))*100))
   end
end
                    for _,v in pairs(game.Players:GetChildren()) do
                        
				if v == chosen(1,#game.Players:GetChildren()) then

					v.Team = redTeam
					humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
					v.CameraMode = "LockFirstPerson"
					v.CameraMaxZoomDistance = 9

					table.insert(RedTeamCount, v.Name)

					if nameGui then
						nameGui.name.TextColor3 = redTeamColor
					end

					print(v.Name .. " put in ".. v.Team.Name)

				for _,v in pairs(game:GetService("Players"):GetChildren()) do if v ~= chosen then
               

					v.Team = blueTeam
					v.CameraMode = "Classic"
					v.CameraMaxZoomDistance = 9

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(BlueTeamCount, v.Name)
					if nameGui then
						nameGui.name.TextColor3 = blueTeam
					end
					print(v.Name .. " put in ".. v.Team.Name)

				end
                end
			end
            end



			--------- if the player dies, it puts them back in the lobby and removes them from the team

			-- when a player has died
			char:WaitForChild("Humanoid").Died:Connect(function()

				plr.Team = lobbyTeam
				plr.CameraMode = "Classic"
				plr.CameraMaxZoomDistance = 9


			end)

		end	

	end	
end)

Main Problem

char:WaitForChild("Humanoid").Died:Connect(function()

				plr.Team = lobbyTeam
				plr.CameraMode = "Classic"
				plr.CameraMaxZoomDistance = 9


			end)

		end	

	end
end)

I advise that you look at the entire script. Thanks for reading

do you have a lobbyspawn?

when they die it seems you just set team to lobby and your not actually moving them back to a spawn

probably put it right below this line

They don’t even get set to the team lobby. It was working before I rewrote the script. There is lobby team btw

do you get any errors when running this script?

1 Like

There are no errors in the script or output

I added a print debug next to char:WaitForChild and in char.WaitForChild and both of them didnt’t print

print("Not Printing Char")
                        char:WaitForChild("Humanoid").Died:Connect(function()
                             
                print("Working")
                plr.Team = lobbyTeam
                plr.CameraMode = "Classic"
                plr.CameraMaxZoomDistance = 9
 
 
            end)
 
        end 
 
    end
    end
end)

you have a few issues here if i am reading right you are looping through all players then inside that loop you are looping through all players again. on top of that you are also using the same player variable v which i believe is even in the main loop which also loops through all players but using the variable plr

you probably only need 1 for loop going through the players if you make the connection there to the character it may work better

Also use player.CharacterAdded:Connect(function() something like this

plr.CharacterAdded:Connect(function(Player)
	local Character = Player.Character
	local humanRoot = Character:WaitForChild('HumanoidRootPart')
	humanRoot.CFrame = LOBBYSPAWN.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3)
end)
1 Like

i think you can try simply using the classic game.plr:getplr() instead of a variable which i dont see anywhere in the script get updated otherwise due to some sneaky issues in roblox’s part you are instead getting an char check from an outdated variable

for i, plr in pairs(game.Players:GetPlayers()) do

			local char = plr.Character

Ok so you want me to do this?

for _,v in pairs(game.Players:GetChildren()) do
                        
				if v == chosen(1,#game.Players:GetChildren()) then

					v.Team = redTeam
					humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
					v.CameraMode = "LockFirstPerson"
					v.CameraMaxZoomDistance = 9

					table.insert(RedTeamCount, v.Name)

					if nameGui then
						nameGui.name.TextColor3 = redTeamColor
					end

					print(v.Name .. " put in ".. v.Team.Name)
     end
end

				for _,v in pairs(game:GetService("Players"):GetChildren()) do 
                           if v ~= chosen then
               

					v.Team = blueTeam
					v.CameraMode = "Classic"
					v.CameraMaxZoomDistance = 9

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(BlueTeamCount, v.Name)
					if nameGui then
						nameGui.name.TextColor3 = blueTeam
					end
					print(v.Name .. " put in ".. v.Team.Name)
      end
end

I added ends to end the for loops so they dont loop together

I just tested and I got the same issue, I made the for loops seperate and it still didn’t work. I am very confused ):

you could try using the characteradded function i added to my post above. doing this will wait until the character respawns then move them to the lobbyspawn but you will need to change that to the actual lobbyspawn brick

Yeah but the thing is it doesn’t run at all. Theres something inside the loop thats causing it to loop forever (thats my guess)

well you do have 3 loops doing the same exact thing

so say you had 10 players for each player it loops then inside that the next for loop loops through all 10 players and for each player in that it loops through all players in the next loop if I am reading it all right

also the connection to the character died is only being made inside the InRound value change imo you should use the above that i posted characteradded inside of PlayerAdded function just setup straight in the script

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Player)
		local Character = Player.Character
		local humanRoot = Character:WaitForChild('HumanoidRootPart')
		humanRoot.CFrame = LOBBYSPAWN.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
	end)
end)

just put that in one the main scripts for the game make sure to change the lobbyspawn to the right object