I’m doing a project where a random number is generated from 0 to 9 and the Player has to press the number that is generates on their keyboard, however I’m having difficulty detecting if they don’t press the correct number. It currently detects the mouse and letters from the keyboard whereas I only want it to detect the numbers, I also then want it to say which number they did press instead of the correct one.
Example:
It generates number 1 but they press 2, it will say “You pressed 2 but you were supposed to press 1”
(along the lines of that)
TLDR; how do I make it ignore KeyBoard Inputs (Apart from numbers) and ignore mouse inputs (mouse clicks etc)
local random = math.random(0, 9) -- random number
local text = ""
if random == 0 then text = Enum.KeyCode.Zero end
if random == 1 then text = Enum.KeyCode.One end
if random == 2 then text = Enum.KeyCode.Two end
if random == 3 then text = Enum.KeyCode.Three end
if random == 4 then text = Enum.KeyCode.Four end
if random == 5 then text = Enum.KeyCode.Five end
if random == 6 then text = Enum.KeyCode.Six end
if random == 7 then text = Enum.KeyCode.Seven end
if random == 8 then text = Enum.KeyCode.Eight end
if random == 9 then text = Enum.KeyCode.Nine end
script.Parent.TextLabel.Text = tostring(random) -- shows number it generated (user should press this number)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == text then -- if they pressed correct number
if Selected[1].Name == "TopRight" then -- this is just for the mini game
local Rotation = script.Parent.Frame.Rotation
if Rotation > 309 and Rotation < 351 then
script.Parent.Parent:Destroy()
print("Success!")
else
end
end
end
end
end)
Just make an array of KeyCodes that are ok to use, then just check if the KeyCode they pressed is inside that array.
local validKeys = {Enum.KeyCode.Zero, Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three, Enum.KeyCode.Four, Enum.KeyCode.Five, Enum.Keycode.Six, Enum.KeyCode.Seven, Enum.KeyCode.Eight, Enum.KeyCode.Nine}
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
--Check if the key is valid, before continuing.
if table.find(validKeys, input.KeyCode) then
if input.KeyCode == text then -- if they pressed correct number
if Selected[1].Name == "TopRight" then -- this is just for the mini game
local Rotation = script.Parent.Frame.Rotation
if Rotation > 309 and Rotation < 351 then
script.Parent.Parent:Destroy()
print("Success!")
else
end
end
end
end
end
end)
That works perfectly, although how would I make it say, "You pressed key whereas you were supposed to press key"? As with that it would count the entire keyboard instead of just the numbers, as it would say
"You pressed W whereas you were supposed to press 1"
Alright well do you have any GUI set up? If so then heres a script for that:
local GUI = YourGUILocationHere
local validKeys = {Enum.KeyCode.Zero, Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three, Enum.KeyCode.Four, Enum.KeyCode.Five, Enum.Keycode.Six, Enum.KeyCode.Seven, Enum.KeyCode.Eight, Enum.KeyCode.Nine}
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
--Check if the key is valid, before continuing.
if table.find(validKeys, input.KeyCode) then
if input.KeyCode == text then -- if they pressed correct number
--Note, you need a text label for this to work--
GUI.YourTextLabelNameHere.Text = "You pressed the right number!"
if Selected[1].Name == "TopRight" then -- this is just for the mini game
local Rotation = script.Parent.Frame.Rotation
if Rotation > 309 and Rotation < 351 then
script.Parent.Parent:Destroy()
print("Success!")
else
GUI.YourTextLabelNameHere.Text = "You pressed " .. UIS:GetStringForKeyCode(input.KeyCode) .. "whereas you were supposed to press" .. UIS:GetStringForKeyCode(text)
end
end
end
end
end
end)
Quite simple, once you set up the GUI stuff all I did was use UserInputService to convert the KeyCode into a string. UserInputService:GetStringForKeyCode()