If statement or variable not working

I have a script that detects when your team changes. Here it is:

The first time when it printed “Spectating” was when I Joined the game. The MaxZoomDistance was at 30 and the CanCollide part was false. Just as it should be.

The second time when it printed “Night Guard” was when I changed my team to Night Guard. The MaxZoomDistance went to 0.5 as it should be. The only thing was the CanCollide part was still false. The if statement is suppose to make the CanCollide part true if your team is “Night Guard.” How come it stays false?

Could you show your code in Text (not really necessary but it would be better than reading the code in a screenshot)

if newTeam.Name == "Night Guards" then

You really only have to pay attention to the parts I circled and the explanation below the image, but I’ll show you the script in text form. :wink:

local players = game:GetService("Players")
local player = players.LocalPlayer

function onTeamChange(newTeam)

	if newTeam == "Spectating" then
		player.CameraMaxZoomDistance = 30
	else
		player.CameraMaxZoomDistance = 0.5 --0.5
	end
	print(newTeam)
	if newTeam == "Night Guard" then
		game.Workspace.Misc.InvisNG1.CanCollide = true
	else
		game.Workspace.Misc.InvisNG1.CanCollide = false
	end
end

onTeamChange(player.Team)
player:GetPropertyChangedSignal("Team"):Connect(function()
	onTeamChange(player.Team)
end)
1 Like

I don’t understand why you posted that? You didn’t give any explanation for that and also I never put a team called “Night Guards.” I didn’t misspell it.

try doing newTeam == game.Teams["Night Guard"] and newTeam == game.Teams["Spectating"] because you are seeing if the team is equal to a string and not the actual team and the parameter you pass to the function is the players team not the team name OR do newTeam.Name == “Night Guard” and newTeam.Name == “Spectating”

newTeam is a Team object not a string, you have to get it’s name which is a string, hense newTeam.Name == “Night Guards”

Also applies for newTeam.Name == “Spectating”

you are trying to compare an instance to a string

use “.Name”

1 Like

the newTeam value is a string though. It printed out as a string, and it’s also able to compare the “Spectating” team just fine. Putting .Name on it won’t work and will return an error.

This is not true. when you print out an object it will print out the name of it in the output

1 Like

Roblox studio prints instances by taking their name. Hover over it and it will be underlined, click it and it will show it in the explorer.

right here is proof I’m right
the value type is team not string

Oh you’re right. Thank you.

I don’t know why put the first time I put “.Name” it returned an error. Guess it works now!

1 Like