How would I go about making a key binds settings menu and UI ,so a player can change any action to a key that he wants
I made this awhile ago. I’ll show my script:
['BindableKey'] = function(self, object, initFunction, moveFunction, releaseFunction, status, connectionName, key, bindFunction)
local displayButton = object:FindFirstChild('Display') -- display would be the TextButton that handles input
if displayButton then
self.BindedKeys[key] = connectionName -- a table inside of the modules that handles keys that are already binded so no 2 keys can be binded to the same key
maid.ActiveConnections[connectionName] = userInputService.InputBegan:Connect(function(input, isSystemReserved) -- maid just handles connections, a place for connections to connect and disconnect as needed
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode.Name == key and not isSystemReserved then
bindFunction() -- bindFunction is a function inside of the ModuleScript inside of the UI object
end
end)
displayButton.Text = key -- display the key, this is where it's initially shown after data is loaded
displayButton.MouseButton1Click:Connect(function() -- when the button is clicked, prepare for an input
local oldButton = displayButton.Text -- "archive" the old key
displayButton.Text = '> <' -- show to the user that it is ready for input
if maid.ActiveConnections[connectionName] then -- if there's an old connection that would be in place of the current one then disconnect
maid.ActiveConnections[connectionName]:Disconnect()
end
maid['BindFunction'..connectionName] = userInputService.InputBegan:Connect(function(input, isSystemReserved) -- finds the old connection for the button and disconnects it
if input.UserInputType ~= Enum.UserInputType.MouseMovement then
maid['BindFunction'..connectionName]:Disconnect()
end
if input.UserInputType == Enum.UserInputType.Keyboard and not isSystemReserved then
local inputName = input.KeyCode.Name -- W, A, S, D, E, F, Spacebar etc
self:UnbindByConnectionName(connectionName) -- UnbindByConnectionName is a function that just clears the old input inside of the module
if not self:IsBinded(inputName) and inputName:len() == 1 then -- if the length is 1, it has to be a letter. if it's a comma, period, semicolon etc... it won't work
displayButton.Text = inputName -- input the new name
self.BindedKeys[key] = nil -- remove the old key from the list of binder keys
self.BindedKeys[oldButton] = nil
self.BindedKeys[inputName] = connectionName
maid.ActiveConnections[connectionName]:Disconnect() -- find the old connection and disconnect it
maid.ActiveConnections[connectionName] = userInputService.InputBegan:Connect(function(newInput, isSystemReserved) -- the new connection. my connection name for example is "Reset"
if newInput.UserInputType == Enum.UserInputType.Keyboard and newInput.KeyCode.Name == inputName and not isSystemReserved then -- basically checks if the input is the same and then calls it as needed
bindFunction(newInput.KeyCode.Name)
end
end)
if releaseFunction then -- function that is called when the key is re-binded (I save it to datastore so that's what it's mainly for
releaseFunction(inputName)
end
else
displayButton.Text = oldButton -- if the key is already binded or the name is longer than 1 key, revert to the previous valid key
end
else
displayButton.Text = oldButton
end
end)
end)
else
return warn('No DisplayButton object for',object)
end
end;
3 Likes
Wow thank you so much i will definitely look over your code and break it down to understand why it works thank you so much
1 Like
Also would this go in a local script and where would I put it
I handle all of my UI elements in a ModuleScript so there probably, but you could also handle them in a LocalScript as a child of each of the elements. It’s really up to you haha
2 Likes
once again thank you so much really do appreciate it
1 Like