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

How Do I Print when The First Player in The Table Touches part, something happens?

So Basically, if the first player in the table Clicks the Part, a print should appear "Hello" but why it's not working? I get no error

local playersInRedRoom = {}

RedClickDetector.MouseClick:Connect(function(player)
    if #playersInRedRoom < 2 then
    table.insert(playersInRedRoom, player.Name)

    local redWinPart = workspace.RedRoom.Win.ClickDetector
redWinPart.MouseClick:Connect(function(click)
    if click.Name == playersInRedRoom[1] then
        print("hi")
    end
    end)

1 answer

✓ Accepted answer
  1. You forgot to add an "end" for your first "if" statement. You can add it on line 6.

  2. I must also mention that there's a problem on line 9. Because you are looking for the name of the player who clicked the ClickDetector, you want to put after the two = signs this: playersInRedRoom[1].Name

Notice that I added Name. That is because if you just look for the first player in the table, it's not going to equal click.Name. Why? Well because a player does not equal their name. You must do click.Name == playersInRedRoom[1].Name; otherwise, it would always print nil instead of "hi".

I will show you what I am talking about by editing your Script:

local playersInRedRoom = {}

RedClickDetector.MouseClick:Connect(function(player)
	if #playersInRedRoom < 2 then
        	table.insert(playersInRedRoom, player.Name)
	end -- Added an end right here to close your if statement

local redWinPart = workspace.RedRoom.Win.ClickDetector
redWinPart.MouseClick:Connect(function(click)
	if click.Name == playersInRedRoom[1].Name then -- Added name right after [1] because that is what the if statement is looking for. It is looking for the player's name, not the player
        	print("hi")
    	end
end)

Hope this helps!

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