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

How To Check if Player That Pressed the key is in the table?

So basically, if the player is on the table, and pressed the key W, the print should appear "Player 1 pressed the key" However, even tho I join in the table, i still get the print "Player is not in the table" any help?

ServerScript

local remote = game.ReplicatedStorage.RemoteEvent
local theTable = {}


workspace.SpawnLocation.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(theTable, hit.Parent)
	print(hit.Parent.Name.." Joined the game")
	end
end)


remote.OnServerEvent:Connect(function(player, theplayer)
		if theplayer.Name == table.find(theTable, 1) then
			print("Player 1 pressed the key")
	else
			print("Player is not in table")
		end
end)

Local Script

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local remote = game.ReplicatedStorage.RemoteEvent


UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W then
		print("W was pressed")
		remote:FireServer(player)
end
end)

1 answer

Hit.Parent allegedly corresponds to the Character Rig of a Player if contact is made between one and a BasePart; you're trying to compare a string with a userdata. Firstly, use Hit.Parent.Name to store the Character's handle, for appropriate comparison, or compare thePlayer's avatar to the corresponding Character. Secondly, table.find's second argument pertains to the attribute the function is trying to locate within the specified array. 1 is likely not going to be in your table, so query for thePlayer's name instead, or as I mentioned, it's avatar.

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