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

How do I determine the player who input the key in UserInputService?

So, for a game, I wanted to use a function that fires every time a player presses one. Except, it must be a specific player. Is there any way that I can figure out the player that is doing the input, during the event of InputBegan and InputEnded?

1 answer

UserInputService is a service only accessible in localscripts, and localscripts can also view a property of the Players service that tells you what player the script is owned by

local player = game.Players.LocalPlayer -- the player

local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function()
	print(player.Name..' pressed something')
end)

2 comments

0

Are you sure that they can only be accessed in LocalScripts? I used it on a normal script, and it worked perfectly, but only while running in Studio.

0

Yes, It'll work in studio but as soon as you do a server test or run it in game it won't work. It's best to not ever use it in server scripts

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