Script to Detect Team Change not Working

local Teams = game:GetService("Teams")    
local Players = game:GetService("Players")    

if game.Players.LocalPlayer.Team == ("Innocent")    
then    
    print("Test")    
end    

My script isn’t working, any idea why?

2 Likes

Uh some variables weren’t called, and should’ve been called because it was already defined and should’ve been used. Also I don’t see why anyone would put parenthesis around a string for some reasons. Team under LocalPlayer is a Class, Team is not supposedly comparable to a string because it’s already a Class.

You could also do this instead:

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

if LocalPlayer.Team == Teams["Innocent"] then
	print("ez team")
else
	print("no team :(")
end

So in order to find something that is comparable to that string, presently known as “Innocent”, is we can do:

if LocalPlayer.Team.Name == "Innocent" then
	print("ez team")
else
	print("no team :(")
end

Why Team.Name? Well that’s because .Name is a property of Team. Hope that helps

2 Likes

I’ve edited some parts here and there, for a brief explanation. I’ve already tried this in a separate .rbxl file.

There’s more to detecting team change than that. What you have above will only check the player’s team once, when the script first runs. But, we want to check every time the player’s team changes. We can use events for this. We know that the Player class (the class of the LocalPlayer object) has a property called Team, which is the Team object that the player is currently in. We can use the GetPropertyChangeSignal function to get a signal that fires whenever the Team property changes.

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

We’ll create a function called onTeamChange that’ll run every time the player’s team changes. This will simplify the process of handling team changes.

function onTeamChange(newTeam)
    if newTeam.Name == "Innocent" then
        print("Innocent")
    else
        print("Not innocent")
    end
end

We can now implement this function

onTeamChange(localPlayer.Team)
localPlayer:GetPropertyChangedSignal("Team"):Connect(function()
    onTeamChange(localPlayer.Team)
end)

Note that we’re running the function once in the beginning, incase the player joins and is already in a team before the script loads.

10 Likes

The Script (In Server Script Service)

What happens in game (Clearly on Innocent on the side)

1 Like

LocalPlayer is only accessible in LocalScript known as a Client-Sided Script, it does not have any relations with Server-Sided Scripts (Scripts) which will not work and never will.

1 Like

Ah, where would I put the local script?

1 Like

Use this in the LocalScript instead and place it anywhere that associates with Client-Sided Scripts such as:

StarterCharacterScripts, StarterPlayerScripts, StarterGui and StarterPack.

I recommend you also use what OptimisticSide gave because it’s useful whenever you need to check a team change happening with the player. It’s similar to mine, but mine is an example code. Have a good day sir!

2 Likes