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
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
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)
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)
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.