Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

GUI won't open with this local script and FE is enabled why?

-- NOTE: FE IS ENABLED --

Alrighty, so I'm working on something called a Spawner. It basically takes an item from a folder and pops it into the workspace. Here's the local script:

local plr             = game.Players.LocalPlayer
local CAS             = game:GetService("ContextActionService")
local RS              = game:GetService("ReplicatedStorage")
local spawnerEvent    = RS:WaitForChild("Events"):WaitForChild("Spawner")
local maxMatches      = 20
local gui             = script.Parent
local container       = gui:WaitForChild("Container")
local foundItem       = container:WaitForChild("FoundItem")
local list            = container:WaitForChild("List")
local search          = container:WaitForChild("Search")
local Inventory_Items = RS:WaitForChild("Items")
local Cooldown        = false
local CooldownTime    = 1

function onKeyPress(actionName, userInputState, inputObject)
	print("K PRESSED!")
	if userInputState == Enum.UserInputState.Begin then
		print("!")
		container.Visible = not container.Visible
	end
end


function updateList()
	for _,v in pairs(list:GetChildren()) do
		
		v:Destroy()
		
	end
	
	local results = {}
	
	for _,Item in pairs(Inventory_Items:GetChildren()) do
		if (string.lower(Item.Name):match(string.lower(search.Text)) or string.lower(Item.StringName.Value):match(string.lower(search.Text))) and #search.Text >= 1 then
			table.insert(results, Item)
		end
	end
	for index, Found in pairs(results) do
		if index <= maxMatches then
			local Copy = foundItem:Clone()
			local ItemName = Copy:WaitForChild("ItemName")
			Copy.Visible = true
			ItemName.Text = Found.StringName.Value
			Copy.Position = UDim2.new(0, 10, 0, (36 * index) - 25)
			Copy.Name = Found.Name
			Copy.Parent = list
			ItemName.MouseEnter:connect(function()
				ItemName.TextColor3 = Color3.new(255/255, 170/255, 0/255)
			end)
			ItemName.MouseLeave:connect(function()
				ItemName.TextColor3 = Color3.new(255/255, 255/255, 255/255)
			end)
			ItemName.MouseButton1Click:connect(function()
				if not Cooldown then
					print("Spawning item...")
					Cooldown = true
					search.Text = "Search Items"
					spawnerEvent:FireServer(plr, "cloneObject", Found)
					wait(CooldownTime)
					Cooldown = false
				else
					print("Clicked, cooldown.")
					search.Text = (CooldownTime > 1) and "Please wait " .. tostring(CooldownTime) .. " seconds..." or "Please wait " .. tostring(CooldownTime) .. " second..."
					updateList()
				end
			end)
		end
	end
end


search.Changed:connect(function()
	updateList()
end)

CAS:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.K) -- When you press K the GUI opens. But, it's not opening. How can I fix this?

updateList()

To be more precise, when the K key is pressed a GUI is supposed to open. But the GUI doesn't open. So, how can I fix this?

Thanks!

3 comments

0

Try UserInputService

0

Well, I don't want it to open while players are typing.

0

You can check if the key press was recognized while the players were typing with the return argument "gameProcessedEvent" which is a bool value (with UserInputService)

1 answer

Like I suggested in my comment, you could try UserInputService

local UIS            = game:GetService("UserInputService")

local plr             = game.Players.LocalPlayer
local CAS             = game:GetService("ContextActionService")
local RS              = game:GetService("ReplicatedStorage")
local spawnerEvent    = RS:WaitForChild("Events"):WaitForChild("Spawner")
local maxMatches      = 20
local gui             = script.Parent
local container       = gui:WaitForChild("Container")
local foundItem       = container:WaitForChild("FoundItem")
local list            = container:WaitForChild("List")
local search          = container:WaitForChild("Search")
local Inventory_Items = RS:WaitForChild("Items")
local Cooldown        = false
local CooldownTime    = 1

UIS.InputBegan:connect(function(input,gameProcessedEvent)    
    if input.KeyCode == Enum.KeyCode.K and not gameProcessedEvent then
        print("K Pressed!")
        container.Visible = not container.Visible
    end
end)

function updateList()
    for _,v in pairs(list:GetChildren()) do

        v:Destroy()

    end

    local results = {}

    for _,Item in pairs(Inventory_Items:GetChildren()) do
        if (string.lower(Item.Name):match(string.lower(search.Text)) or string.lower(Item.StringName.Value):match(string.lower(search.Text))) and #search.Text >= 1 then
            table.insert(results, Item)
        end
    end
    for index, Found in pairs(results) do
        if index <= maxMatches then
            local Copy = foundItem:Clone()
            local ItemName = Copy:WaitForChild("ItemName")
            Copy.Visible = true
            ItemName.Text = Found.StringName.Value
            Copy.Position = UDim2.new(0, 10, 0, (36 * index) - 25)
            Copy.Name = Found.Name
            Copy.Parent = list
            ItemName.MouseEnter:connect(function()
                ItemName.TextColor3 = Color3.new(255/255, 170/255, 0/255)
            end)
            ItemName.MouseLeave:connect(function()
                ItemName.TextColor3 = Color3.new(255/255, 255/255, 255/255)
            end)
            ItemName.MouseButton1Click:connect(function()
                if not Cooldown then
                    print("Spawning item...")
                    Cooldown = true
                    search.Text = "Search Items"
                    spawnerEvent:FireServer(plr, "cloneObject", Found)
                    wait(CooldownTime)
                    Cooldown = false
                else
                    print("Clicked, cooldown.")
                    search.Text = (CooldownTime > 1) and "Please wait " .. tostring(CooldownTime) .. " seconds..." or "Please wait " .. tostring(CooldownTime) .. " second..."
                    updateList()
                end
            end)
        end
    end
end


search.Changed:connect(function()
    updateList()
end)

updateList()

2 comments

0

This doesn't work.

0

^ When I tried it, it worked.

This discussion is preserved as it was. New answers, comments, and votes are closed.