Different way to use "or"

Hey, I was wondering how many way to use “or” exist !
And how can I make this script more efficiently :

if tostring(Player.Team) == "a4" or tostring(Player.Team) == "a6" or tostring(Player.Team) == "a7" or tostring(Player.Team) == "a11" or tostring(Player.Team) == "a12" or tostring(Player.Team) == "a15" or tostring(Player.Team) == "a25" then
2 Likes

You can use a dictionary to check,

In your example it would be like something like this:

local ValidTeams = {
	["a4"] = true,
	["a6"] = true,
	["a7"] = true,
}

if ValidTeams[tostring(Player.Team)] then

end
14 Likes

Make a dictionary table and check the team

``
local Teams = {
[“A”] = Team1
}

``

1 Like

If you watch to check if the team name is any ‘a’ followed by some digits, you can use string pattern matching as well. E.g.

function is_an_a_team(team: Team) boolean
    return tostring(team):match("^a%d+$") ~= nil
end

if is_an_a_team(Player.Team) then
     -- do something with the 'a' team
end
3 Likes

Ok thanks you very much @dthecoolest and @MadionChooi !
Have a nice day ! :+1:

No that not what I want to achieve here the full code :

local Players = game.Players
local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

Player:GetPropertyChangedSignal("Team"):Connect(function()
	if tostring(Player.Team) == "a4" or tostring(Player.Team) == "a6" or tostring(Player.Team) == "a7" or tostring(Player.Team) == "a11" or tostring(Player.Team) == "a12" or tostring(Player.Team) == "a15" or tostring(Player.Team) == "a25" then
		Humanoid.UseJumpPower = false
		Humanoid.JumpHeight = 0
	else
		Humanoid.UseJumpPower = true
	end
end)
1 Like

Then the other suggestions are definitely better suited I’d say

1 Like

Don’t really need named indexes for this, table.find() will do.

local Players = game.Players
local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Teams = {"a4","a6","a7","a11","a12","a15","a25"};

Player:GetPropertyChangedSignal("Team"):Connect(function()
	if table.find(Teams, Player.Team) ~= nil then
		Humanoid.UseJumpPower = false
		Humanoid.JumpHeight = 0
	else
		Humanoid.UseJumpPower = true
	end
end)
2 Likes

For what it’s worth, dictionaries actually work extremely well here. Though it doesn’t feel quite proper and it’s certainly not what I’d use in other languages, Roblox dictionaries with string keys have a flat access time where table.find takes more time the larger the table is.

Edited to fix. Thanks RoBama.

Average time to check if a random key exists in a 5,000 key table with a 50% chance of the key existing:
table.find - 016.81µs
string index - 0.13µs
Average time to check if a random key exists in a 50,000 key table with a 50% chance of the key existing:
table.find - 164.06µs
string index - 0.18µs

So the average time to check if a key exists in a table grows linearly with the size of the table when using table.find. Using a dictionary there is an almost flat access rate.
The point isn’t that dictionaries should be used here where the table is really short and the time to access doesn’t matter, the point is that dictionaries shouldn’t be thought of as overkill in scenarios like this.

3 Likes

Linearly, not exponentially! Although it’s a bit academic at this point, but w/e :stuck_out_tongue:

You 10x’d the input size and 10x’ the number of repetitions. Correct way to test this would be just 10x’ing the input size.

EDIT: Wait I thought you did 5000 and 50000 searches, might be reading it wrong.

Anyway search should at worst be linear.

EDIT 2: nvm I read it right the first time.

2 Likes