Script not working for whatever reason

I am currently making a custom hotbar, and when the user presses 1 on their keyboard, slot number 1 should be selected using a UIStroke. For some reason the code runs (i put a print debug) but doesn’t set any of the values.
Code Snippet:

if obj.KeyCode == Enum.KeyCode.One then
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA("UIStroke") and v.Name == "SelectedStroke" then
v.Enabled = false
elseif v:IsA("BoolValue") and v.Name == "IsActivated" then
v.Value = false
end
end -- everything before the elseif under this won't do anything
SelectedStroke.Enabled = true
Slot1.IsActivated.Value = true
elseif obj.KeyCode == Enum.KeyCode.One and Slot1.IsActivated.Value == true then -- this works
Slot1.SelectedStroke.Enabled = false
Slot1.IsActivated.Value = false
end

I get no errors

local userInputService = game:GetService("UserInputService")

local keys = {
	[Enum.KeyCode.One] = script.Parent.Slot1,
	[Enum.KeyCode.Two] = script.Parent.Slot2,
	[Enum.KeyCode.Three] = script.Parent.Slot3,
	[Enum.KeyCode.Four] = script.Parent.Slot4,
}

-- get the UIStroke and set the parent to nil
local stroke = sctipt.Parent.UIStroke
stroke.Parent = nil

userInputService.InputBegan:Connect(function(input, processed)
	-- if the input was processed return and do nothing
	if processed == true then return end
	-- if the input is not a keyboard return and do nothing
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	-- try to get the slot from the keys table
	local slot = keys[input.KeyCode]
	-- if the slot is nil return and do nothing
	if slot == nil then return end
	-- set the strokes parent to the selected slot
	stroke.Parent = slot
end)
1 Like

It works, but how can I make it so if I press for the same key again it completely disappears?

Example:

I pressed 4 its selected, press 4 again and now the selection box’s parent is nil.

local userInputService = game:GetService("UserInputService")

local keys = {
	[Enum.KeyCode.One] = script.Parent.Slot1,
	[Enum.KeyCode.Two] = script.Parent.Slot2,
	[Enum.KeyCode.Three] = script.Parent.Slot3,
	[Enum.KeyCode.Four] = script.Parent.Slot4,
}

-- get the UIStroke and set the parent to nil
local stroke = sctipt.Parent.UIStroke
stroke.Parent = nil

userInputService.InputBegan:Connect(function(input, processed)
	-- if the input was processed return and do nothing
	if processed == true then return end
	-- if the input is not a keyboard return and do nothing
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	-- try to get the slot from the keys table
	local slot = keys[input.KeyCode]
	-- if the slot is nil return and do nothing
	if slot == nil then return end
	-- if the stroke parent is already the slot then set to nil if not then set it to the slot
	if stroke.Parent == slot then
		stroke.Parent = nil
	else
		stroke.Parent = slot
	end
end)