The code is executed 1 time

This is my first time writing here sorry if i make mistakes

I’m creating a key mapping GUI (so the player can map a key however they want to execute a function)

but it turns out that the first time I click on the button to assign its key it works fine, but the second time I try to press it I have to double click so that the function can be executed

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    that the code executes well

  2. What is the issue?
    the script works only 1 time fine

  3. What solutions have you tried so far?
    I searched the forums and still can’t find the answer.

here is my script

local UIS = game:GetService("UserInputService")
local ReplicatedStoraged = game:GetService("ReplicatedStorage")

local assignKeyButton = script.Parent
local AssignatedFrame = script.Parent.Parent.Parent
local cancel = script.Parent.Parent.Cancel
local close = script.Parent.Parent.Close


local UserInputService = game:GetService("UserInputService")
local object = script.Parent

assignKeyButton.Text = "Click Here"

local button = object.Activated:Connect(function()
	local keyAssign ; keyAssign = UserInputService.InputBegan:Connect(function(Key)
		assignKeyButton.Text = "..."

		close.Activated:Connect(function()
			keyAssign:Disconnect()

			assignKeyButton.Text = "Click Here"
			AssignatedFrame.Visible = false
		end)

		cancel.Activated:Connect(function()
			keyAssign:Disconnect()
			assignKeyButton.Text = "Click Here"
		end)

		if Key.KeyCode.Name == "Unknown" then
			return
		else
			assignKeyButton.Text = Key.KeyCode.Name

			local KeyAssign = ReplicatedStoraged.Keys:WaitForChild("Left")

			KeyAssign.Value = Key.KeyCode.Name
			keyAssign:Disconnect()
		end
	end)
end)
1 Like

You’re nesting quite a lot of event connections, make sure you’re disconnecting them appropriately otherwise your game will suffer from memory leaks and the issue of which you are facing.

1 Like