MouseButton1Click Still Runs After It's Finished Executing

Alright, so this might sound kind of stupid but I’m trying to make a Keybind Script. The Script is parented to a Button inside of a frame. So when i run this script it checks for a left click on the button, then gets the key that was pressed on the keyboard, and saves it to a variable. This works fine however I have 4 of these (for 4 keybinds) and when i click on the next buttons the key for the previous button updates to the button i just pressed?! I literally do not even know how that makes sense because the button click should end. Any help will be appreciated thanks! :smiley:

In the end, I want all 4 buttons to save individual keybinds, and not just one for all 4 buttons!

Here are a few screenshots followed by the code itself:
Note: Please keep in mind that I’m not a GUI designer!

→ This is what it should look like

→ The first key changes, like it should
image

→ After the first keybind changing you can see how all buttons are showing the same key

If you change just 1 key out the previous ones you already custom assigned a keybind THEY WILL ALL CHANGE TO THE ONE YOU JUST PRESSED!

Code for the first button: (they are all the same just the numbers are different, eg key1, key2 etc)

local UserSettings = require(game.ReplicatedStorage.UserSettings)
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Text = "Press Any Key"
	
	mouse.KeyDown:connect(function(key1)
		UserSettings.Key1 = key1
		script.Parent.Text = "Key 1: "..key1
	end)
end)

UserSettings is just a module script. It is where it stores the keybinds that were pressed.

I have tried going on the web, trying to fix it myself, using debounces, using mouseup and a lot of other things. (can’t really remember)

again, any help will be appreciated thanks!! :smiley: - MasterWindowz7And10

You dont end the KeyDown function. It essentially runs forever, therefore you need a return
Try see if your code works…

mouse.KeyDown:connect(function(key1)
UserSettings.Key1 = key1
script.Parent.Text = "Key 1: "…key1
return
end)

I have already tried break as a solution, it just sends an error that it can only be used in a loop. Return does not work either.

Try this:

local UserSettings = require(game.ReplicatedStorage.UserSettings)
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

 -- one thing i noticed is that you have to separate the two events, as it is connecting the KeyDown every time you do script.Parent.MouseButton1Click, making the KeyDown run multiple times every time you click it
-- you could either disconnect before you make a new connection, or just move the keydown event
-- i also changed KeyDown to InputBegan just because its more well rounded

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Text = "Press Any Key"
end)
game:GetService("UserInputService").InputBegan:Connect(function(input)
    local keyname = tostring(input.KeyCode):match("KeyCode.(.+)")
	UserSettings.Key1 = keyname 
	script.Parent.Text = "Key 1: " .. keyname 
end)

I think that instead of using
7
I think you should use “UIS”

Sadly, I tried this and i didnt even have to click the button for it to register the key input. So I got rid of the end so the input function will fire only after i press the button, but this causes the same issue. As for “disconnecting” I do not understand.

I tried that but it didn’t do anything.

@MasterWindowz7And10 This should do the trick.

local UserSettings = require(game.ReplicatedStorage.UserSettings)
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

script.Parent.MouseButton1Click:Connect(function() -- when clicked
    script.Parent.Text = "Press Any Key"

    local userInputConnection
    userInputConnection = game:GetService("UserInputService").InputBegan:Connect(function(input) -- listens for input, returns a connection. As long as the connection isn't disconnected, this function will run whenever a input is detected.
        userInputConnection:Disconnect() -- disconnectes the function, so this function wont run again
        local keyname = input.Name -- name of the input
        UserSettings.Key1 = input  -- set the input
        script.Parent.Text = "Key 1: " .. keyname -- display the input
    end) -- done
end)
1 Like

This has fixed the problem where it keeps detecting inputs afterwards, however it now says Key 1: InputObject instead of Key 1: (whatever key i pressed).

Also, I want the letter that was pressed (like q,w,o,p) to be passed to the variable in the module script in case it wasn’t already. (Just to clarify)

replace the variable “keyname” with this:

local keyname = tostring(input.KeyCode):match("KeyCode.(.+)")
1 Like

Oh, sorry about that.

local keyname = input.KeyCode.Name
2 Likes

This was already solved thanks though! :smiley:

2 Likes