While scrolling through the forums, I noticed someone trying to make a script that identified a team with the last player “alive” in the server. I thought it would be some good practice to try to make this, so I did. The demo works as follows: Each team has a bool value assigned to it indicating the team’s “alive” status. There is a part located in the workspace for me to walk up to and touch. Upon doing so, all bool values will be set to false aside from that of the team belonging to the player that touched the part, indicating that the player that touched the part is the last player “alive.” A gui/frame/text label are then created indicating the team that won.
However, when I was done with the demo, I noticed I had some bizarre issue where the team I was fetching from the player that triggered a touched event was not the same as the player’s actual team. Below is some more info on what I have going on.
My current set up is as follows:
There are 3 teams, Red, Green, and Blue.
In each team, there is a bool value (called “Value” by default) set to true
There is a Part in workspace that has a script (not local) that identifies the player that touches the part, sets the bool values in all the teams that are not the player’s team to false, then creates an output indicating the winner. In this case, it creates a GUI in PlayerGUI.
The script located in the part is as follows:
script.Parent.Touched:Connect(function(part)
local success, player = pcall(function()
return game.Players:GetPlayerFromCharacter(part.Parent)
end)
local Teams = game.Teams:GetTeams()
if success then
print(player.Team)
for _, i in ipairs(Teams) do
if tostring(player.Team) ~= tostring(i) then
i.Value.Value = false
else
UI = Instance.new("ScreenGui")
UI.Parent = player.PlayerGui
Frame = Instance.new("Frame")
Frame.Parent = UI
Frame.Size = UDim2.new(0, 300, 0, 200)
Label = Instance.new("TextLabel")
Label.Parent = Frame
Label.Size = UDim2.new(0, 300, 0, 200)
Label.Text = "Team " .. tostring(i) .. " wins."
end
end
end
end)
I read the documentation for a few of the classes I was using, but I didn’t find anything that solved my problem. I also read a few of the related topics generated while creating this post since this is my first post. Similarly, those didn’t yield any results.
I’m always willing to provide more info if need be.
(Edit): Some more information on the bug, on line 7, I print the player’s team to the console, which I fetched through getting the player’s Player
from their character model, then getting that Player
's team. However, every time I do this, regardless of what that Player
's team is, the red team is output to the console.
(Solution): All the teams are of the same BrickColor. Because Roblox uses this to differentiate between teams, when Roblox tried to identify a specific team of mine when they all had the same BrickColor, it would think all of my teams were the same team and default to team red.