Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help with this Search GUI?

Asked by 8 years ago

I was experimenting with Search GUI's and came up with this, now I was wondering if someone can tell me what I am doing wrong with it as to why it's only working if I type the full text? I want it to be visible if I type stuff like C and there's a frame named Cow.

Here's what I tried:

local frame = script.Parent.Parent.ScrollingFrame

script.Parent.Changed:connect(function()
    for _,v in pairs(frame:GetChildren()) do
        if string.find(script.Parent.Text,v.Name) then
            v.Visible = true
        else
            v.Visible = false
        end
    end
end)

How would I make it work if I typed stuff like "C"?

Any help appreciated.

Sorry I can't explain any better.

2 answers

Log in to vote
0
Answered by 8 years ago

I noticed that my old answer was deleted for some reason. Never mind, I will take from Perci1's answer. I hadn't considered using string.match() for a search script before.

This is the hierarchy used in this script:

  • ScreenGui
    • Frame
      • Frame (Called "Cow")
    • Script
    • TextBox (for searching)
    • TextButton ('search' button)

Script:

local searchText = "Search for your object here" -- text that you want your search box to display
local p =script.Parent
local f  = p.Frame
local children = f:GetChildren()
local text = p.Textbox
local button = p.TextButton
cow.Visible = false
text.Text = searchText

function search(txt)
    if txt ~= " " and txt ~= "" and txt ~= searchText then -- checks that the search is valid
        for _,v in pairs (children) do
            local name = v.Name:lower()
            text = txt:lower()
            local str = name:match(txt)
            if str then
                v.Visible = true
            end
        end
    else
        text.Text = "Invalid search"
    end
end

button.MouseButton1Down:connect(search(text.Text) -- fires when clicked

This should work. There was no rush this time, but I don't have access to Studio right now.

Ad
Log in to vote
-1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

You can use string.match instead of string.find. It will return the first match found, or nil if there isn't a match.

local str = "Cow"
print( str:match("C") ) -->C
print( str:match("Hi") ) -->nil

Read more on string patterns.

0
This isn't what he asked for, although it can be used like this in a more complicated manner. He wanted to know how to find a Frame called 'Cow' without typing the full name. TheDeadlyPanther 2460 — 8y

Answer this question