Why is this team-picking script not working for some people?

This script allows new players to a server to pick a team they want to play as in my game. Recently I did an update where teams are randomized in each server.

When I did this update, there’s been an issue where I’d say 20% of players can’t pick a team. I’ve heard it’s worse for mobile players, but I’ve tested myself on both mobile and PC and I never had an issue joining, even if the server wasn’t empty. When this happens nothing appears on their dev consoles and they have the ClickError sound play when they try to pick a team.

What could the issue be?

function start()
	for i,v in ipairs(script.Parent:GetChildren()) do
		if v.ClassName == "ImageButton" then
			v.MouseButton1Click:Connect(function()
				local locked = false
				if game.Teams[v.Name]:FindFirstChild("locked") ~= nil then
					if game.Teams[v.Name].locked.Value == true then
						local who = game.Teams[v.Name].locked:WaitForChild("creator")
						if not game.Players.LocalPlayer:IsFriendsWith(who.Value.UserId) then
							locked = true
						end
					end	
				end
				if locked == false then
					if game.Players.LocalPlayer.Team == game.Teams["For Recruitment"] and mkt:UserOwnsGamePassAsync(game.Players.LocalPlayer.userId,11322392) then
						if #game.Teams:FindFirstChild(v.Name):GetPlayers() < 4 then
							switch(v)
						else
							script.Parent.Parent.ClickErrorSound:Play()
						end
					elseif 	game.Players.LocalPlayer.Team == game.Teams["For Recruitment"] and not mkt:UserOwnsGamePassAsync(game.Players.LocalPlayer.userId,9579784) then
						if #game.Teams:FindFirstChild(v.Name):GetPlayers() < 2 then
							switch(v)	
						else
							script.Parent.Parent.ClickErrorSound:Play()
						end
					else
						script.Parent.Parent.ClickErrorSound:Play()
					end
				else
					script.Parent.Parent.ClickErrorSound:Play()
					game.StarterGui:SetCore("ChatMakeSystemMessage", {
						Text = "[ERROR]: You are not friends with the person(s) on this team!";
					})
				end	
			end)
		end
	end
end

start()

game.Teams.ChildAdded:Connect(start())
2 Likes

Is this in a local script or server script?

1 Like

Local script. So that’s why (I’m assuming) it affects some but not others

1 Like

Might want to use remote events to send the server info because of filtering enabled.

1 Like

Already does that. When you click the button to join a team, it fires to a remote to switch the player’s team. It’s definitely not that part since the script seems to break before it has the chance to fire, since in the script it would delete the UI after that, which doesn’t happen.

Where does it fire the remote event?

switch(v)

function switch(v)
	print("this works")
	game:GetService("ReplicatedStorage"):FindFirstChild("Remote"):FindFirstChild("TeamEvent"):FireServer(game.Teams:FindFirstChild(v.Name))	
	print("this ok")
	script.Parent.Parent.Parent.Parent.info.Visible = true
	script.Parent.Parent.Parent.Parent.Parent.buttons.Enabled = true
	script.Parent.Parent.Parent.Parent.team_Pick:Destroy()
	game.StarterGui:SetCore("ChatMakeSystemMessage", {
		Text = "[TIP]: You can lock your team to friends only by saying /lockteam!";
	})
end

Where is this? Is it in the same script?

yes, it is. It’s above it. Should it be below it?

There is so many if statements. This is not the reason why it doesn’t work, but it makes it harder to read. You can put multiple statements in a line such as “If a = b and c = d then”

i know i intend to clean the code up at a later date but right now, I am trying to make it work and go from there

It makes it a lot harder to find the problem and where it is when there are so many statements.

You need to do it through the server.