How to make HTTPservice script update based on a stringvalue?

Hello. I have a gui that uses HTTPservice to get data from another website. It’s a weather gui and what I want it to do is use a textbox to search a location, and then get data from that location. I’ve done this by using a stringvalue, so that whatever you put in the stringvalue is the location the script requests. I’ve not been able to create a textbox that searches a location, and have tried countless times, but still have not found anything. I want the script to update the string value and HTTPscript. This is my current script:
Script Image

The variables aren’t the real thing, but it is meant to represent the scenario that I am currently in. I need some help on making a script that works, because I couldn’t find anything on the devforums or Google.

You don’t even need a StringValue for this. Seeing as your code needs some cleanup I wrote one for you. Did this on mobile so it might have mistakes.

LocalScript - modify to your liking

local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local ScreenGui = script.Parent
local searchBox = ScreenGui.TextBox
local searchButton = ScreenGui.TextButton
local RemoteEvent = ReplicatedStorage RemoteEvent


    -- do anything you'd like with the result

ServerScript - modify to your liking

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local url = "" -- enter api url
local RemoteEvent = ReplicatedStorage.RemoteEvent

function getCityResult(query: string)
    local success, data = pcall(function()
        return HttpService:GetAsync(url .. query)
        -- query is just a term to look up the city.
        -- might be needed in the url
    if not success then
        return -- yea too lazy to handle errors
    local response = HttpService:JSONDecode(data)
    return response

RemoteEvent.OnServerEvent:Connect(function(player, query)
    local result = getCityResult(query)
    RemoteEvent:FireClient(player, result)

repeat wait() until game.Players.LocalPlayer, come on, you don’t need this useless loop. The script will load alongside the player, so what’s it there for?

You can simply use TextBox.Text and ignore the StringValue unless you need to store it somewhere. Otherwise you can use a table.

Additionally, don’t do script.Enabled = false. Even if you disable it it’ll continue to run until the thread finishes, you can just slap a return there and it’ll be void, aka, it does nothing, and it will exit the function.

Plus, please use task.wait() instead of wait().


Answer 1:

I would recommend that you create a function that runs whenever the StringValue is changed. This function would be similar to this: local StringValue = game.StarterGui.ScreenGui.StringValue StringValue.Changed:Connect(function(newValue) – Logic here if newValue == “search” then – Get search results from web end end) The above code connects a function to the Changed event, which is fired whenever the StringValue‘s value is changed. In this function, we check if the new value of the StringValue is “search”, and if it is, we get the search results from the web.

Answer 2:

You can do this by simply checking if the textbox is equal to the new string value. local gui = script.Parent local stringvalue = gui.StringValue local textbox = gui.TextBox stringvalue.Changed:Connect(function(newValue) if newValue == textbox.Text then --Do the HTTP stuff end end)