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

How To Detect If First/Second Player of the Table Pressed the Key?

Edit: I just want to know, how to check which player Pressed the key

So Basically, If First player on the table, Pressed the key, let's say a print happens. How can I Do That?

I tried = if i[1].key.KeyCode == Enum.KeyCode[playersInRoomOne] then but it's not working, I don't get any errors

Local Script

local remote = game.ReplicatedStorage.RoomsStringValues.RoomOne.RoomOneEvent
local UIS = game:GetService("UserInputService")
local playerOneString = game.ReplicatedStorage.RoomsStringValues.RoomOne.PlayerOne
local playerTwoString = game.ReplicatedStorage.RoomsStringValues.RoomOne.PlayerTwo
local playersInRoomOne = {playerOneString, playerTwoString}


local allTheKeyWords = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X","Y","Z"}
local randomKeyChooser = allTheKeyWords[math.random(1, #allTheKeyWords)]

local isThereAWinner = false


print("Players in game are: "..table.concat(playersInRoomOne, ", "))

UIS.InputBegan:Connect(function(key)
-- The Code should be here for player 1
	if key.KeyCode == Enum.KeyCode[randomKeyChooser] then
		print("Player 1 pressed it")
	else
		print("Player 2 pressed it")
	end
	
end)

4 comments

0

LocalScript -> Remote Event/Remote Function -> Server Script. You can only detect input from a specific player using a LocalScript. If you want to send the input to the server, you will have to use a RemoteEvent to send the input, and a ServersScript to handle the repsonse.

0

I know that, this is a local script, now pls read my question, and if you know the answer, let me know

0

Please put more information so people know that this is a LocalScript as there is nothing that shows you are using a LocalScript.

0

Ok My Bad, can you help me :/

2 answers

killerbrenden is probably right. You'd probably want a remote that sets a variable to true, that variable meaning that the player pushed the button. However, if this is the right answer, you might wanna get killerbrenden to put an answer in, cause it's what he suggested

2 comments

0

I get that as I said on the comments, My Question is, How can I check if first player pressed the Key? Forget About Remotes and other stuff

0

exactly like i said, use a variable, use UserInputService to detect if the player pressed the key, and when the key's pressed, set the variable to true, repeat for the other player

I'll take a guess. A pretty good way is creating a blank table:

local blank = {}

I used a click detector in place, but it should work the same.

Get when the player pressed the key (or in my case clicked the part) and create a function:

script.Parent.ClickDetector.MouseClick:Connect(function(player)

end)

Put in the function something like this:

table.insert(blank,player.Name)

for i, v in pairs(blank) do
	
	print(i,v)
	
end

The total script:

local blank = {}

script.Parent.ClickDetector.MouseClick:Connect(function(player)

table.insert(blank,player.Name)

for i, v in pairs(blank) do
	
	print(i,v)
	
end

end)

1 comment

0

Uh not really, I don't know if I'm not clear enough of what I'm asking, I want to check who Clicked the Key, The Player's Name...

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