What do I want to achieve? Basically the script is supposed to work so if you walk onto the basketball court while a game is going on and your not on either team, you get teleported outside of the court.
What is the issue? The script is not working with checking if whether or not the player is on a team. It teleports you outside of the court regardless of whether or not you’re on a team.
What solutions have you tried so far?
Here are the two methods I’ve tried so far for checking if a player is on a team when they hit the basketball court.
local OnAteam = false
if game.Teams.Red:FindFirstChild(hit.Parent.Name) then
print("it worked")-- checking if player is on red team (not working)
OnAteam = true -- if player is on a team, this comes back true
end
print(OnAteam) -- comes back false again
Second Method:
local OnAteam = false
for i, v in pairs (game.Teams:WaitForChild("Red"):GetChildren()) do
print(v.Name)
if v.Name == hit.Parent.Name then
OnAteam = true
end
end
print(OnAteam) -- comes back false
I was on the red team when I went into court and each time it came back false.
ROBLOX has some trouble checking team names against Player.Team. I have worked a lot with teams in my games and in the past I have found success comparing team colors rather than team names. This has the unfortunate side effect of not being able to use two teams with the same TeamColor
local Teams = game:GetService("Teams")
local TeamOne = Teams:WaitForChild("Red")
local TeamTwo = Teams:WaitForChild("Blue")
local function playerIsOnTeam(player)
return (player.TeamColor == TeamOne.TeamColor) or (player.TeamColor == TeamTwo.TeamColor)
end
print(playerIsOnTeam(player))
To avoid such side effects, you can determine if the player’s team is the same as the one in game.Teams by doing something like this
local Teams = game:GetService("Teams")
local TeamOne = Teams:WaitForChild("Red")
local TeamTwo = Teams:WaitForChild("Blue")
local function playerIsOnTeam(player)
return (player.Team == TeamOne) or (player.Team == TeamTwo)
end
print(playerIsOnTeam(player))
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
game.Players.PlayerAdded:Connect(function(player)
if player.Team == Teams["(team name)"] then -- add, or Teams["(team name)"] to add more teams to it
end
end)