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 use number input keys in scripts to trigger them (EX. Weapon swap)?

What I want to do is use the # keys on the keyboard to trigger certain scripts. (Preferably 2 in this case)

local mouse = game.Players.LocalPlayer:GetMouse()
local emp = false

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) -- Run function
	if key == "2" then   <---[[PROBLEM RIGHT HERE. I want this to be 2, it works with "q", but I need numbers, not letters]]
		emp = true
		print("EMPon")
		local character = script.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		player.EMPon = true
		if player.EMPon == true then
			EMPvictim = player.EMPvictim
			repeat wait()
			until EMPvictim.Value ~= nil
			victim = game.Players:FindFirstChild(""..EMPvictim.Value)
			player.PlayerGui.EMP.Adornee = victim
		end
		end
end)

1 answer

I believe SetCoreGuiEnabled will allow you to use the number keys if you disable backpack.

Otherwise, try InputBegan

-- Example:
game:service'UserInputService'.InputBegan:connect(function(obj)
if obj.KeyCode == 'One' and obj.UserInputState == 'Begin' then -- I believe this would work.
print'1 was pressed that day.'
end
end)

4 comments

0

How would I script that using the script I wrote? InputBegan look's simpler.

0

HOLD IT I forgot something.

0

Not sure if it would work 100% tho. Give it a shot.

0

Nope. Doesn't work at all :/

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