When I have it print Color
the output says: Bright Blue
, but when I ask if Color = "Bright blue"
it returns that it's not. Please Help
while wait() do Players = game.Players:GetChildren() NumPlayers = game.Players.NumPlayers for i,v in pairs (Players) do Color = (v.TeamColor) if Color == "Bright blue" then print 'success' else print (Color) end end end
Comparing different types of things in Lua
will always result in false
:
print( 1 == "1" ) -- false -- (one is a number, one is a string) print( Workspace == "Workspace") -- false -- (one is a ROBLOX instance, one is a string)
In your case, TeamColor
is a BrickColor value; so you can't compare it with "Bright blue"
, a string.
You have to compare BrickColors:
if v.TeamColor == BrickColor.new("Bright blue") then
Equivalently, you could turn the TeamColor
into a string (but that would be worse)
if tostring(v.TeamColor) == "Bright blue" then -- or if v.TeamColor.Name == "Bright blue" then -- as chess123mate suggested
In general, you want to deal with the most restricted possible types. Lua makes this a little less pleasant, unfortunately.
Still, it's better to talk about BrickColors, since it's harder to, for instance, make a typo in a name1. Strings are more free-form and could have a lot more done to them than BrickColors.
The basic problem is that you can have strings which aren't brick colors -- thus there's a lot more room for weirdness and confusion.
Using BrickColors may be marginally slower, because of the cost of creating a new one. That should not be an argument to use strings. 20% savings on something that should only be taking a very small fraction of your program time doesn't justify using messier/worse code.
If you save 0.1 seconds by using v.TeamColor.Name == "Bright blue"
instead of v.TeamColor == BrickColor.new("Bright blue")
, you're probably calling this hundreds of thousands of times. Why are you doing that? That is the problem you have to solve.
It would make the most sense to have a variable for the team color defined anyway,
oneTeam = BrickColor.new("Bright blue") ..... if v.TeamColor == oneTeam then
This reads the clearest, and will cut most of the cost of BrickColors; it also helps prevent typos.
You should probably use :GetPlayers()
instead of :GetChildren()
.
Meaning BrickColor.new("Bright bleu")
won't make a "valid" BrickColor. Unfortunately, ROBLOX will just give you 'Medium stone grey', but this is probably much easier to catch when you debug. Even better, you could use BrickColor.blue
to avoid the possibility altogether (or in general, making your own variables like this for your pallet) ↩
When you say print(Color)
, the print
command automatically converts its arguments to a string. However, when you compare a Color with a string, you will always get false because they are different types. You have a few options:
if tostring(Color) == "Bright blue" then
or
if Color.Name == "Bright blue" then
or
if Color == BrickColor.new("Bright blue") then
Locked by AmericanStripes, BSIncorporated, SanityMan, Operation_Meme, Redbullusa, and MessorAdmin
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?