Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does game.Players.PlayerAdded:Connect(function() not run? (A bit hard to answer...)

First, my Script takes random teams and adds them to the Teams Service. Then it assigns the chosen Teams to variables. After that I wrote a game.Players.PlayerAdded:Connect - function. Why does it not run? Before adding the part that chooses random Teams it did run. Heres my Code:


local nan = 0

repeat
    local index = math.random(1,#Teamfolder)
    currentteam = Teamfolder[index]
    print(currentteam.Name.." was chosen for this round.")
    currentteam.Parent = game.Teams
    table.remove(Teamfolder, index)
    wait(1)
    nan = nan+1
until nan == 5

TeamsLoaded = true
	
game.Teams.ChildAdded:Connect(function(Child)
if  Child == game.Teams.Hunters--1
then
Hunters = game.Teams.Hunters
end
	
if Child ==game.Teams.Necromancers--2
then
	Necromancers = game.Teams.Necromancers
end

if Child ==game.Teams.Arsonists--3
then
	Arsonists = game.Teams.Arsonists
end

if Child ==game.Teams.Jesters--4
then
	Jesters = game.Teams.Jesters
end

if Child ==game.Teams.Witches--5
then
	Witches = game.Teams.Witches
end

if Child ==game.Teams.Seers--6
then
	Seers = game.Teams.Seers
end
end)

local Teams = game.Teams:GetTeams()

local Remotes = game.ReplicatedStorage.Remotes
GameStart = game.ServerStorage.GameStarting
Neededplayers = 3 --Needed Players to start.


game.Players.PlayerAdded:Connect(function(plr)	--[[I closed the function to make it less confusing. (I'll provide the code inside it below)]]
end)

Code inside function:

repeat
	wait()
until TeamsLoaded == true
print("Player joined: "..plr.Name)
	if #Players:GetPlayers() == Neededplayers
	then
		print("")
		print("Enough Players: (" ..#game.Players:GetPlayers().."/"..Neededplayers..")")
		print("")
		print("Assigning Teams...")
		for _,player in pairs(Players:GetPlayers())--For every player do:
		do
			local randomteam = Teams[math.random(1,#Teams)]--Get random Team.
			local Full = randomteam:FindFirstChildWhichIsA("IntValue")--Get IntValue inside the randomteam.
			if #randomteam:GetPlayers() < Full.Value --Check if Maximum capacity is not filled.
			then
				player.Team = randomteam --If it isn't, insert player.
			else
	    if #randomteam:GetPlayers () >= Full.Value --Check the other case.
			then
				print(randomteam.Name.." is full. Cannot insert "..player.Name.." . Looking for other Teams...")
			--Search for Team which isn't full:
				repeat 
					randomteam = Teams[math.random(1,#Teams)]
					Full = randomteam:FindFirstChildWhichIsA("IntValue")
				until #randomteam:GetPlayers() < Full.Value
				player.Team = randomteam --Insert the player. 
				wait(1) --Debounce to prevent Lag.
			end
		 end
		end
--Printing Teams:		
        print("")
        print("Teams:")
        print("")
		for _,team in pairs(Teams)
        do
		print(team.Name)
		print(table.unpack(team:GetPlayers()))
	end
	GameStart.Value = true
else --Not enough players.
		print("Not enough Players: ("..#game.Players:GetPlayers().."/"..Neededplayers..")")
end

The things inside How do you fix it? Thanks in Advance.

1 answer

✓ Accepted answer

Try putting it at the beginning of your script, and add a few debug prints so you know if it's running in the first place. There isn't any reason the event isnt connecting. Also it only runs when a new player joins. The first players that join wont effect it.

3 comments

0

It worked for some reason. Thanks!

0

Sorry to annoy you, but all other Teams than Werewolves and Villagers are nil

0

Nvm i fixed it

This discussion is preserved as it was. New answers, comments, and votes are closed.