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 check if a specific player presses a key?

as the title said, how do i check if a specific player presses a key? Ive tried to do

local cool = {"Cill_Roblox"}
local gui = script.Parent

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == cool then
		if game:GetService("UserInputService"):IsKeyDown(Enum.KeyCode.L) then
			gui.Enabled = true
		end
	end
end)

but that doesnt work since playeradded only checks once as far as i know and

local cool = {"Cill_Roblox"}
local gui = script.Parent
local guy = false
local uis = game:GetService("UserInputService")

game.Players.PlayerAdded:Connect(function(player)
	if player.Name == cool then
		guy = true
	end
end)

uis.InputBegan:Connect(function(input, gameProccesedEvent)
	if input.UserInputType == Enum.KeyCode.L then
		if guy == true then
			gui.Enabled = true	
		end
	end
end)

ive tried looking stuff like "how to check if specific player presses a key roblox" but found nothing on this so far. anyone know something about this? any help is appreciated

1 comment

0

UIS does not function on the server, it's client-only

1 answer

✓ Accepted answer

first of all, you need to make sure that the UserInputService part is in a local script as it's client-side only, second of all, try using the InputBegan event to toggle the gui instead of checking if a key is being held down.

example:

gui = script.Parent
userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input, gameProcessed)
    if input.KeyCode == Enum.KeyCode.L and game.Players.LocalPlayer.DisplayName == "Cill_Roblox" then
        gui.Enabled = true
end)

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