Why is this script not detecting if someone is in a team

  1. What do you want to achieve? That my (local) script detects if a player joins a team and then do stuff

  2. What is the issue? It’s not doing anything when we come to the part of detecting if the team has changed

  3. What solutions have you tried so far? I’ve tried looking but nothing worked

This is in a local script in StarterGUI

My Code:

local Player = game.Players.LocalPlayer
local Team = game.Teams

if Player.Team == Team.Police then
	print("Police!")
end

Provide feedback if needed!

1 Like

change this line to this:

local Teams = game.Teams

Also put the script in StarterPlayerScripts

I did that but it’s still not printing it To be clear: Teams get changed in a UI (I thought that may be helpful)

EDIT: The code is now this:

local Player = game.Players.LocalPlayer
local Teams = game.Teams

if Player.Team == Teams.Police then
	print("Police!")
end
1 Like

Oh, so if a player presses the police button it will print police?

No, when a player presses that button the team changes. Then it should print that. (Sorry for being so unclear)

Hello! Your script isn’t working since it’s in StarterGui and a LocalScript, meaning it only detects a team change on a client (only possible with exploits). Instead, try this Server Script ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    if player.Team.Name == 'Police' then
        print(player.Name..' is on the "Police" team!')
    end
end)

Please leave a like and mark this as a solution if it solves your problem.

It just get the error:

So ye…

Why would this help? This would cause crashes, a coroutine is much better for fixing this issue.

For some reason, this doesn’t work… I don’t get an error or anything. According to the leaderboard in Studio, it does changes

Try this instead:

Place this LocalScript in StarterPlayerScripts

function CheckTeam()
    if game.Players.LocalPlayer.Team.Name == 'Police' then
        print('Police')
    end
end

local checkteam = coroutine.create(CheckTeam)
coroutine.resume(checkteam)

Please mark this as the solution if it helps and leave a like!

1 Like

What team are they spawning on?

They spawn on the Prisoner team

He wants it in StarterGui, IMO it doesnt matter if an exploiter changed their team, unless the team has special tools/abilitys

1 Like

Alright, I’ve found the issue!

Here’s the script

-- Make sure to create the TeamsService!

local Player = game.Players.LocalPlayer
local Teams = game:GetService("Teams")

script.Parent.MouseButton1Down:Connect(function()
     player.Team = Teams.Police
     print("Police!")
end)

How will this help? you have just changed the variable.

2 Likes

My way will prevent exploiters. Also you spelt abilities wrong lol

That’s wouldn’t work…? He doesn’t want it for GUI? Are you sure your a programmer?

1 Like

He stated in a previous reply that he did.

How a player will be able to be in a team if team doesnt even exists?
He probably have team service or it would show the error.

Its probably because that player isnt in the right team.

use Instance.new() or create it in the explorer