I am experiencing a strange issue where when the TextBox is being changed (The Text) two Frames visibility should change. One of them being the default one and the other one the new frame. However the Frames won’t go invisible at all.
script.Parent.SearchBox:GetPropertyChangedSignal("Text"):Connect(function()
local searchText = script.Parent.SearchBox.Text
if searchText ~= "" then
script.Parent.SearchFrame.Visible = true
script.Parent.ScrollingFrame.Visible = false
else
script.Parent.SearchFrame.Visible = false
script.Parent.ScrollingFrame.Visible = true
end
end)
make sure you’ve placed this code in the right place within your script and that the references to the frames (SearchFrame and ScrollingFrame ) are correct.
local players = {} -- Create a table
script.Parent.SearchBox:GetPropertyChangedSignal("Text"):Connect(function() -- start when the text is changed
script.Parent.ScrollingFrame.Visible = false
script.Parent.SearchFrame.Visible = true
for _, player in ipairs(game:GetService('ReplicatedStorage').Vehicles.Sedans:GetChildren()) do -- get a list of all players
players[player.Name] = player.Name -- absolutely no clue
end
for _, name in pairs(players) do
if (script.Parent.SearchBox.Text:lower():sub(1, 4) == name:sub(1, 4)) then -- if the text upto 4 characters matches then
for _, v in pairs(script.Parent.SearchFrame:GetChildren()) do
if script.Parent.SearchFrame:FindFirstChild(name) then
else
local clone = script.Parent.CloneThisSearch:Clone()
clone.Parent = script.Parent.SearchFrame
clone.Name = name
clone.VehicleName.Text = name
clone.Visible = true
end
end
else
for _, v in pairs(script.Parent.SearchFrame:GetChildren()) do
if v:IsA("Frame") and v.Name ~= "CloneThisSearch" then
v:Destroy()
end
end
script.Parent.SearchFrame.Visible = false
script.Parent.ScrollingFrame.Visible = true
end
end
end)
Your frames are being created and destroyed in quick succession. Which causes the frames to not be correctly updated in terms of visibility and their names.
can you try this code
local function updateSearchResults(searchText)
local nameMatches = {}
for _, player in ipairs(game:GetService('ReplicatedStorage').Vehicles.Sedans:GetChildren()) do
local playerName = player.Name
if searchText:lower():sub(1, 4) == playerName:sub(1, 4) then
table.insert(nameMatches, playerName)
end
end
script.Parent.ScrollingFrame.Visible = #nameMatches == 0
script.Parent.SearchFrame.Visible = #nameMatches > 0
-- Clear existing search results
for _, v in pairs(script.Parent.SearchFrame:GetChildren()) do
if v:IsA("Frame") and v.Name ~= "CloneThisSearch" then
v:Destroy()
end
end
-- Create new search result frames
for _, name in ipairs(nameMatches) do
local existingFrame = script.Parent.SearchFrame:FindFirstChild(name)
if not existingFrame then
local clone = script.Parent.CloneThisSearch:Clone()
clone.Parent = script.Parent.SearchFrame
clone.Name = name
clone.VehicleName.Text = name
clone.Visible = true
end
end
end
script.Parent.SearchBox:GetPropertyChangedSignal("Text"):Connect(function()
local searchText = script.Parent.SearchBox.Text
updateSearchResults(searchText)
end)
Also remember to adjust the code as needed to match the exact hierarchy and names of your gui elements.