Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Am I using a table right?

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

✓ Accepted answer

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

8 comments

0

So then I would put if allied==true  then -- code here ?

0

Yes. Or, to be shorter and clearer, just if allied then

0

I originally posted 'if allied then' but I just used what I know. Thanks for the help

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?

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.

0

oh, thanks!

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.

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

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

5 comments

0

Everything else was pretty good

0

Wouldnt it be                        for,v in pairs(allies) do                       or something?

0

oh yeah, do. sorry xD

0

Didn't work "Unable to cast Array to int"

0

Oh, I'm unfamilliar with that then.

This discussion is preserved as it was. New answers, comments, and votes are closed.