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

Help with an admin door, help with admins table?

When I touch a part, it prints both and my name is FiredDusk in studio mode...

Admins = {
"Player1", 
"FiredDusk"
}

script.Parent.Touched:connect(function(hit)
	for i,v in pairs(Admins) do
		if hit.Parent.Name == v then
			print'YAY'
		else
			print'BOO'
		end
	end
end)

1 comment

0

try elseif hit.Parent.Name ~= v instead of else

2 answers

✓ Accepted answer

I see your problem.

Let me explain the problem before giving you the answer.

If an admin touches the part, it will print Yay, but it will also print Boo. This is because you're not stopping the loop when an admin shows up. Even if you do stop the loop, the Admin may not be first in the list, so it will still print Boo. I would recommend using a function to check instead, as it will be much easier.

Example,

Admins = {
	"Player1", 
	"FiredDusk"
}

local function CheckAdmin(name)
	for i,v in pairs(Admins) do
		if name == v then
			return true;
		end
	end
	return false;
end

script.Parent.Touched:connect(function(hit)
	if CheckAdmin(hit.Parent.Name) then
		print("Yay")
	else
		print("Boo")
	end
end)

What this does,

This will call a function with the parameter "name". It will loop through the table, and if it finds a value in the table that matches the name, it returns true, which stops the function. If it gets through the table and no admins exist, it returns false.

Good Luck!

1 comment

0

How can I do this without the function? I am asking this because I am not good with 'return' and I don't understand them. Please try to make it w/o a function if you can.

Your script works perfectly fine, it's only showing both because Player1 is in the table.

Your code loops through each variable of the table, adn then decides whether or not hit.Parent.Name is equivalent to that. Since there is nobody name Player1 in your game, it will print BOO everytime that the brick is touched because it has to go through the entire table.

You can either remove the BOO part, or remove Player1 from your table. Both of these will fix your problem

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