Having problems with a If Statement

Hi, I’m wanting to change a color for a beam, based off a player’s team. I have triple checked my code, googled but I’m still having trouble.

In the script I have printed the player’s team color as well as if the if statement is being run(which is not)
OUTPUT:

  • Bright blue (printed) / Bright red (printed)
  • Skipped If Statement (printed)
script.Parent.Fire.OnServerEvent:Connect(function(player)
local color = player.TeamColor
	print(color)
	if color == "Bright red" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new({ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(159, 0, 13)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(159, 0, 13))
		})

	elseif color == "Bright blue" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new({ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(9, 0, 158)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(9, 0, 158))
		})
	else
		print("Skipped If Statement")
	end
end)
-- Note this isn't the full script

I might of forgot to put more info you need more please ask thanks.

I think it may be because of

I don’t think it should have the paratheses, should be this:

ColorSequence.new{ }

Player.TeamColor has the value type of a BrickColor, not a String. That means to check if a color is a specific BrickColor in an if statement, you’ll have to construct a BrickColor through BrickColor.new().

So instead of if color == "Bright red", it will be if color == BrickColor.new("Bright red"), and so on.

  1. I used the same thing to change a part in my script and it worked
	local TCP = script.Parent:WaitForChild("TeamColorparts")
	if color == "Bright red" then
		color = BrickColor.new(21)
	elseif color == "Bright blue" then
		color = BrickColor.new(23)
	end
  1. I don’t think It would make sense to use BrickColor.new
    BrickColor.new

Constructs the closest BrickColor that can be matched to the specified RGB components.

The script is skipping the statement

I am a bit confused here, what parts are being skipped and can you show me the full script for reference?

It would make sense to use BrickColor.new(), as that’s how you construct & check for a specific BrickColor.

If that second code you showed in the reply works, then the color variable there must have been a String and not a BrickColor.

I forgot to add this to it

	local color = player.TeamColor
	local TCP = script.Parent:WaitForChild("TeamColorparts")
	if color == "Bright red" then
		color = BrickColor.new(21)
	elseif color == "Bright blue" then
		color = BrickColor.new(23)
	end

Yes, but once its in a variable doesn’t it make it a string/int?

  1. The if statement getting skipped
  2. My rest of my script isn’t help that much but here you go
script.Parent.Equipped:Connect(function()
	local char = script.Parent.Parent
	local player = game.Players:GetPlayerFromCharacter(char)
	local color = player.TeamColor
	local TCP = script.Parent:WaitForChild("TeamColorparts")
	if color == "Bright red" then
		color = BrickColor.new(21)
	elseif color == "Bright blue" then
		color = BrickColor.new(23)
	end
	TCP.BrickColor = color
	local A = script.Parent.Anim
	local hum = char.Humanoid
	local anim = hum:LoadAnimation(A)
	anim:Play()
end)

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
	FS:Play()
	local RCP = RaycastParams.new()
	RCP.FilterDescendantsInstances = {player.Character}
	RCP.FilterType = Enum.RaycastFilterType.Blacklist
	
	local RR = workspace:Raycast(script.Parent.End.Position,(mousePos - script.Parent.End.Position)* 100,RCP)
	
	local beam = Instance.new("Beam")
	beam.Name = "Beam"
	beam.Parent = script.Parent
	local AT1 =  Instance.new("Attachment",script.Parent.End)
	local AT2B = Instance.new("Part")
	AT2B.Parent = workspace
	AT2B.Name = "Hit"
	AT2B.Transparency = 1
	AT2B.CanCollide = false
	AT2B.Anchored = true
	AT2B.CFrame = CFrame.new(mousePos)
	AT2B.Anchored = true
	if AT2B.Position.Magnitude - script.Parent.End.Position.Magnitude > 100 then
		AT2B.Position = AT2B.Position+(AT2B.Position - script.Parent.End.Position).Unit * 100
	end
	local AT2 =  Instance.new("Attachment",AT2B)
	beam.Attachment0 = AT1
	beam.Attachment1 = AT2
	beam.Enabled = true
	local color = player.TeamColor
	print(color)
	if color == "Bright red" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new{ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(159, 0, 13)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(159, 0, 13))
		}

	elseif color == "Bright blue" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new{ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(9, 0, 158)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(9, 0, 158))
		}
	else
		print("Skipped If Statement")
	end
	
	beam.LightEmission = 1 
	beam.LightInfluence = 1 
	beam.Texture = "http://www.roblox.com/asset/?id=2110270024" 
	beam.TextureMode = Enum.TextureMode.Stretch 
	beam.TextureLength = 1 
	beam.TextureSpeed = 1 
	beam.ZOffset = 0
	beam.FaceCamera = true
	beam.Width0 = 1

	-- parent beam
	beam.Enabled = true
	wait(0.25)
	AT2B:Destroy()
	beam:Destroy()
	if RR then
		local hitPart = RR.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
       if model then
			model.Humanoid.Health -= 50
			end
		end 
end)

BrickColor Codes (roblox.com)
BrickColor (roblox.com)
Player (roblox.com)

script.Parent.Fire.OnServerEvent:Connect(function(player)
	local color = player.TeamColor.Name
	print(color)
	if color == "Bright red" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new({ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(159, 0, 13)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(159, 0, 13))
		})

	elseif color == "Bright blue" then
		print("Entering If Statement")
		beam.Color = ColorSequence.new({ 
			ColorSequenceKeypoint.new(0, Color3.fromRGB(9, 0, 158)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(9, 0, 158))
		})
	else
		print("Skipped If Statement")
	end
end)

No, storing something in a variable doesn’t automatically make it string/int. If you store a BrickColor value in a variable, it will remain a BrickColor - and you can check it using BrickColor.new() as I’ve told you in my previous reply.

The reason that the above reply worked (the one marked as the solution), is that in it you’re using player.TeamColor.Name which converts the BrickColor into a string, and then you check it as a string. It’s just an alternative.

You can also convert the BrickColor to its Number value or its Color value, using player.TeamColor.Number and player.TeamColor.Color respectively.