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

My keyboard shortcut script is not working; how would I fix it?

I am making a building game, and I want the player to press E to open the inventory, and while the player is placing a structure, he/she could press Q to cancel the placement.

But, whenever I test the game, none of those keyboard shortcuts work. (Also, I noticed if I have just one keyboard shortcut, (lets say its E), it would work. But how come two keyboard shortcuts in the game don't work?

How would I fix it?

Here is the LocalScript below:

-- When player presses E, show the inventory
game.ContextActionService:BindAction("keyPress", function (actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		if (InventoryShown == false) then
			-- My code to hide the inventory
			return true
		end
		
		if (InventoryShown == true) then
			-- My code to show the inventory
			return true
		end
	else
		return false
	end
end, false, Enum.KeyCode.E)

-- When player presses Q, quit the placement
game.ContextActionService:BindAction("keyPress", function (actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		if (Placing == true) then
			-- My code to quit placing is here
		end
	else
		return false
	end
end, false, Enum.KeyCode.Q)

2 comments

0

Where's the wording to point towards E at the start?

0

Nevermind, found it.

1 answer

I don't know much about this region of roblox lua, but maybe this will help??

Player = game.Players.LocalPlayer
gui = script.Parent
Mouse = Player:GetMouse()
Box = gui.Box -- change what frame it opens here. Change the Box to the frame's name.
Open = false

Mouse.KeyDown:connect(function(key)
 if (key == "e") then
  if (Open == false) then
   Box.Visible = true
   Open = true
  elseif (Open == true) then
   Box.Visible = false
   Open = false
   
end
end
end)
-- created by robloxgameingstudios

Please accept if it does! Your welcome.

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