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

Problems with tables and cloning items into PlayerGui?

I know my RAP might be bad, but that gives no reason to not help...

My main problem here, is that when ever I try to check all of the tables and players if the players name is not valid to the players names in the table, it clones into the players gui, but it clones multiple times when I only want it to clone once! Why is it doing this?

local Ranks = {
	["Rank0"] = {
		"";
		"";
		"";
	};
	["Rank1"] = {
		"";
		"";
		"";
	};
	["Rank2"] = {
		"";
		"";
		"";
	};
	["Rank3"] = {
		"";
		"";
		"";
	};
	["Rank4"] = {
		"";
		"";
		"";
	};
	["Rank5"] = {
		"";
		"";
		"";
	};
	["Rank6"] = {
		"";
		"";
		"Player1";
};
};

for i,v in pairs(game:GetService("Players"):GetPlayers()) do
for q,w in pairs(Ranks.Rank1) do
for a,s in pairs(Ranks.Rank2) do
for _,__ in pairs(Ranks.Rank3) do
for fg,gf in pairs(Ranks.Rank4) do
for hj,jh in pairs(Ranks.Rank1) do
for o,p in pairs(Ranks.Rank2) do
if v.Name ~= w and v.Name ~= s and v.Name ~= __ and v.Name ~= gf and v.Name ~= jh and v.Name ~= p then
script["asd"]:Clone().Parent = v.PlayerGui;
end
end
end	
end
end
end
end
end

If there's another way, please do tell. This is getting very frustrating!

1 comment

0

On line 44 I meant to put Ranks.Rank6

2 answers

Some points:

  • You don't generally need semicolons (though you do need to separate table entries with a comma)
  • Nesting for loops like that is not ideal
  • You missed Rank5 and Rank6 in your nested for loops (accidentally re-using Rank1 and Rank2)

You can simplify your Rank initialization code (though it may not help any if you intend to update it manually instead of using DataStores or something. If that's the case, ignore this bit):

local Ranks = {}
local minRank = 0
local maxRank = 6
for i = minRank, maxRank do
	Ranks["Rank" .. i] = {"", "", ""}
end
Ranks["Rank6"][3] = "Player1"

Next, I would recommend a function that converts the various players in the Ranks into a single lookup dictionary (and also to loop over all the ranks using a single for loop):

local ranksDict
function UpdateDict()
	ranksDict = {}
	for _, rank in pairs(Ranks) do
		for _, player in pairs(rank) do
			ranksDict[player] = true
		end
	end
end

--Later
UpdateDict()
for _, player in ipairs(game.Players:GetPlayers()) do
	if ranksDict[player] then
		--what to do if the player is in the ranks
	else
		--what to do if the player is not in the ranks
	end
end

This way, you don't need to iterate over a whole bunch of Ranks for every player. You go through all the ranks once to create the ranksDict, then you check to see if a player is in it (which is very fast, performance-wise) after that.

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