How to get most up to date result

I have this chunk of code that converts a players name into their Id and then gets their ImageId, and while it works, it’s delayed, which results in incorrect results, as the web requests can take different amounts of time depending on the player.

So I type NinjoOnline, and this is the output
the final result isn’t my player, it gets it a lot earlier than some of the other results, but it uses the last return to base the image off. So how can I stop those web requests if I still changing text?

		local Recipient = HUD.SendMail.Container.TopBar.RecipientBar.TextBox.Text
		if Recipient ~= "" then
			-- Make sure recipient is an actual player
			local UserIdSuccess, UserId = pcall(function()
				return Players:GetUserIdFromNameAsync(Recipient)

			if UserIdSuccess then -- User exists
                local ImageSuccess, UserThumbnail = pcall(function()
                    return Players:GetUserThumbnailAsync(UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)

                if ImageSuccess then
                    HUD.SendMail.Container.TopBar.RecipientImage.Icon.Image = UserThumbnail

                print("Get", Recipient, UserId)

				HUD.SendMail.Container.TopBar.RecipientImage.Icon.Visible = true
				HUD.SendMail.Container.TopBar.RecipientImage.Fail.Visible = false


		HUD.SendMail.Container.TopBar.RecipientImage.Icon.Visible = false
		HUD.SendMail.Container.TopBar.RecipientImage.Fail.Visible = true

Have a button to confirm the userName, userId, and image, and only call the function then.

1 Like

No, I don’t want to add a button. It needs to auto update as you type

Wait sorry, what’s the issue?

Char limit

You could only send the requests after a certain amount of time has passed since the last typed character. Say, 1 second. So the player would type something, but before you send the request, you wait 1 second and see if they type another character. If they do, they haven’t finished typing, but if they don’t, they probably finished typing and you get the image.

Even doing 1 second delay still doesn’t work :confused: I don’t just wanna keep adding more and more time

I implemented something for a stage selector which makes an asynchronous call to the server, a similar structure could work with what you’re doing even though it seems completely unrelated. I’m not sure if it’s the most efficient way but in essence you have a variable where you check if it’s updated between each asynchronous call, and each time the text is updated, it updates the variable. In each function after each async call, you check if the last time the text changed ~= current time change, if it changed then return.

So something like:

local lastChange
    local thisChange = tick()
    lastChange = thisChange
    (do async call)
    if lastChange ~= thisChange then another request was made since the asynchronous call was made so return
    (do next async call)
    if lastChange ~= thisChange then return
    now update text labels' contents

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.