You can write your topic however you want, but you need to answer these questions:
-
I want all teammates to have a marker placed on their head
-
Sometimes markers do not get placed with no error
-
Tried thinking of all the possible solutions, the only one is to just make a loop rather than use listeners
The script below is full (missing some basic declarations), found in a ScreenGui with ResetOnSpawn enabled. I think I have all the necessary connections made as well the necessary disconnections. It should, when you spawn manage all the teammates correctly but somehow a lot of people are missing the markers. Am I doing something wrong?
--Clone and place a marker onto the part
local function placeMarker(part, marker)
local markerClone = marker:Clone()
markerClone.Parent = part
markerClone.Active = true
table.insert(markerCollection, markerClone)
return markerClone
end
--Handle a teammate
local function handleCharacter(character)
local head = character:WaitForChild("Head")
local thisTeammateMarker = placeMarker(head, teammateMarker)
thisTeammateMarker.TextLabel.Text = character.Name
end
--Create a marker adding connection for the provided player
local function setupCharacterAddedConnection(player)
local connection
connection = player.CharacterAdded:Connect(function(characterAdded)
if player.Team == Player.Team then
handleCharacter(characterAdded)
elseif connection then
print("disconnected for different team for " .. player.Name)
connection:Disconnect() --Disconnect if player is no longer a teammate
end
end)
end
--Add markers and make connections for current teammates
for _, teammate in pairs(Player.Team:GetPlayers()) do
if teammate.Character and teammate ~= Player then
handleCharacter(teammate.Character)
end
setupCharacterAddedConnection(teammate)
end
--Listen to new teammates added to create a connection
Player.Team.PlayerAdded:Connect(function(playerAdded)
setupCharacterAddedConnection(playerAdded)
end)