Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use tables to make a team only system? My script won't work. Any help?

Asked by 5 years ago

I am making a team only door, that multiple teams can go through. I could use:

if player.TeamColor = BrickColor.new(teamcolor) then

But adding many teams can make using or very clutered, so this is where I brought tabes in. My current script looks like this: (Please keep in mind that I am new to tables never used them)

local allowedTeams = {
"Cops";
"SWAT";
"FBI";
"Staff";
}

(script)
for i,v in pairs(allowedTeams) do
if player.TeamColor == allowedTeams[v].TeamColor then
(next part of script)

end
end

When I do this it does not work? How would I do this?

1 answer

Log in to vote
0
Answered by
starmaq 1290 Moderation Voter
5 years ago

So first thing, when you use an in pairs loop, the variable that we commonly name i is the index of the member of the table that is getting looped.

--let's say we got this table 
local matable = {"gdfghh", false, 315}

for i, v in pairs(matable) do
print(i)
end

--it'll print 1 2 3 since each time a member of that table passes thorugh the loop it will print its index, when false gets looped for example it will print 2 since that is its index
it's the second member.

Now, the v variable is the value that is getting passed through. so if we loop again.

local matable = {"gdfghh", false, 315}

for i, v in pairs(matable) do
print(v)
end

--it'll print all members of the table, each value will be printed. it'll print "gdfghh" and false and 315.

Now, that's a basic understanding of in pairs loop. You are kind of using it wrongly by doing allowedTeams[v], you gotta do allowedTeams[i] cuz as we said that's the index. And you know you don't even need this. You can simply use v which will be the team that's looping each time.

Also, you're second mistake is, the values found in the allowedTeams table are strings. You pretty much did something similar to this.

"Cops".TeamColor

That will obviously error. You gotta refer to the team object. So this is what you gotta do:

local allowedTeams = {
"Cops";
"SWAT";
"FBI";
"Staff";
}

(script)
for i,v in pairs(allowedTeams) do
if player.TeamColor == game:GetService("Teams"):FindFirstChild(v).TeamColor then


end
end

And that's it!

0
Ah, I see. Thank you. Pizzafireme 14 — 5y
Ad

Answer this question