Problem when changing teams

I’m working on a project, but I’ve run into a problem that I haven’t been able to solve. I tried using Roblox’s AI and other AI tools, but they haven’t been very helpful.

The issue is as follows: when selecting a team through a script connected to a UI located in the StarterGui, the player is correctly assigned to the team. However, to obtain the team’s objects, a ProximityPrompt is used, which checks if the player is part of the team.

The problem is that this ProximityPrompt does not recognize the player as being on the team if they joined through the UI. On the other hand, if the player joins the team via a Spawn with the AllowTeamChangeOnTouch option enabled, or if they are assigned to the team using a Khol’s Admin command, then the ProximityPrompt does detect them correctly and allows interaction.

Script to change to the team
local function assignTeam()
	print("Botón presionado, comprobando whitelist...")
	if whitelist[playerName] then
		local teamName = whitelist[playerName] 
		local assignedTeam = Teams:FindFirstChild(teamName)

		if assignedTeam then
			player.Team = assignedTeam 
			print("Jugador asignado al equipo: " .. teamName)

			local teamAssignedEvent = ReplicatedStorage:WaitForChild("TeamAssigned")
			teamAssignedEvent:FireServer(teamName)

			screenGui.Enabled = false 
			disableEffects()
			task.wait(0.1)
			teleportToSpawn(teamName)

		else
			warn("El equipo '" .. teamName .. "' no existe en Teams.")
		end
	else
		warn("El jugador " .. playerName .. " no está en la whitelist.")
	end
end
Script to get the items in the proximity prompt
-- Referencias a los objetos
local part = script.Parent  -- El Part que tiene el ProximityPrompt
local proximityPrompt = part:WaitForChild("ProximityPrompt")
local serverStorage = game:GetService("ServerStorage")
local equipos = game:GetService("Teams")  -- Accedemos a los equipos
local equipo = "Policía Judicial"  -- Nombre del equipo que puede recibir los items

-- Lista de objetos que queremos entregar
local objetosAEntregar = { 
	"Tarjeta Policía",  -- Estos son los nombres de los objetos en ServerStorage
	"Pistola",
	"Grilletes"
}

-- Función para entregar objetos al jugador
local function darItems(player)
	-- Verifica si el jugador está en el equipo correcto
	if player.Team and player.Team.Name == equipo then
		-- Recorre la lista de objetos y da los objetos desde ServerStorage
		for _, nombreObjeto in ipairs(objetosAEntregar) do
			local objeto = serverStorage:FindFirstChild(nombreObjeto)  -- Busca el objeto en ServerStorage
			if objeto then
				local clon = objeto:Clone()  -- Clona el objeto
				clon.Parent = player.Backpack  -- Coloca el objeto en la mochila del jugador
			else
				warn("No se encontró el objeto: " .. nombreObjeto)  -- Si no se encuentra el objeto, muestra un mensaje de advertencia
			end
		end
	else
		warn(player.Name .. " no está en el equipo de " .. equipo)
	end
end

-- Evento para cuando el jugador interactúa con el ProximityPrompt
proximityPrompt.Triggered:Connect(function(player)
	-- Verificamos si el jugador está en el equipo antes de darle los items
	if player.Team == nil then
		warn(player.Name .. " no está asignado a ningún equipo.")
	else
		-- Si está en un equipo, verificamos si está en el equipo correcto
		if player.Team.Name == equipo then
			darItems(player)
		else
			warn(player.Name .. " no está en el equipo " .. equipo)
		end
	end
end)

-- Escuchar el evento remoto para actualizar el estado del equipo
local replicatedStorage = game:GetService("ReplicatedStorage")
local teamAssignedEvent = replicatedStorage:WaitForChild("TeamAssigned")

teamAssignedEvent.OnServerEvent:Connect(function(player, teamName)
	if player.Team and player.Team.Name == teamName then
		print(player.Name .. " ha sido asignado correctamente al equipo " .. teamName)
	else
		warn(player.Name .. " no está en el equipo " .. teamName)
	end
end)

Do you get any errors in output

No, just debug messages saying the player is not on the team, even though he appears on the team in the tab in the player list.

If the script that changes the player’s team is a LocalScript, then it only changes the team for the current player. The server won’t recognise this change (it can’t see what a LocalScript does) so its own truth tells it that the player is still part of their old team.

Admin scripts run on the server so when you use a command to change the team, everyone sees that the team changed. The best way to see this in action is to run a local server with 2 players, use the team change GUI in the Player1 window, then tab into Player2’s window and see it didn’t change.

The team change GUI needs to fire a RemoteEvent to the server to change the team.

2 Likes

Thank you very much, I will try to make the script global and not local somehow. Or make the RemoteEvent.

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