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

Am I using a table right?

Asked by 10 years ago

I very small knowledge on how to use Tables. From what I have seen in other scripts, this seems what it would look like, i'm not sure. Could someone tell me if this is right, and if not, tell me what to change?

allies=15124, 23235, 23523, 1234, 5235


while true do
    TDS=game.Workspace.TDS
    hos=game.Workspace.hos
    wait()
    game.Players.PlayerAdded:connect(function(plr)
        if plr:IsInGroup(374223) then
            TDS.Value=TDS.Value+1
        elseif plr:IsInGroup(allies) then

        else
            hos.Value=hos.Value+1
        end
    end)
end

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

IsInGroup requires a number, while allies is a list of numbers, so we can't give it allies directly.


We consider a player to be an ally if they are in the first ally group, or the second, or third, and so on.

Therefore we loop over the list of allies and test if they are in the first ally, or the second ally, or the third ally:

local allied = false;
for _, ally in ipairs(allies) do
    allied = allied or plr:IsInGroup( ally );
end

-- `allied` is a boolean; `allied` is true if `plr` is
-- in one of the groups listed in `allies`

For those unfamiliar with this type of for loop, the following snippet is equivalent to the preceding snippet:

local allied = false;
for _ = 1, #allies do
    local ally = allies[_];
    allied = allied or plr:IsInGroup( ally );
end
0
So then I would put if allied==true  then -- code here ? Tempestatem 884 — 10y
0
Yes. Or, to be shorter and clearer, just `if allied then` BlueTaslem 18071 — 10y
0
I originally posted 'if allied then' but I just used what I know. Thanks for the help Tempestatem 884 — 10y
0
I encountered a problem. I had a print(" Hey an ally") in it if they were an ally, and I tested, and it printed 5 times (One for each ally group) Should that happen? Tempestatem 884 — 10y
View all comments (4 more)
0
No. I'm guessing you put the print statement inside of the for loop. It has to happen *after* since you only know the final verdict once you've checked every group. BlueTaslem 18071 — 10y
0
oh, thanks! Tempestatem 884 — 10y
0
It might save some comparisons to add a break statement inside the loop for if allied is true, since you don't have to check if they're in another of the groups if you know that they're already allied. adark 5487 — 10y
0
@adark That's true, though the list should be short enough it doesn't really matter; adding the branch might be more expensive. I didn't want to make it any more complicated than it needed to be either for this example BlueTaslem 18071 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago

This is how you should use the table, it has index which is for example allies , it has numbers it will read and do the script of all of those.

for i,v in pairs(allies) do
if plr:IsInGroup(allies)  then
-- your script o3o

-- Fixed it xD

0
Everything else was pretty good PlatinumLocks 50 — 10y
0
Wouldnt it be                        for,v in pairs(allies) do                       or something? Tempestatem 884 — 10y
0
oh yeah, do. sorry xD PlatinumLocks 50 — 10y
0
Didn't work "Unable to cast Array to int" Tempestatem 884 — 10y
0
Oh, I'm unfamilliar with that then. PlatinumLocks 50 — 10y

Answer this question