Hey, since I am not a very technical person, I am not sure if it’s an issue by design and I just shouldn’t do that or I am just not understanding something correctly, but here’s what happens:
I tested it with mouse clicks and GetUserThumbnailAsync(), as seen in the video. If I call it rapidly (like multiple players clicking at the same time), it breaks the code, by conditions returning wrong numbers and the count repeating. If I do it slowly, it works and counts correctly, maybe because the yielding function is given time to run its course, otherwise, the code gets messed up. Also, the thumbnails are not updating - the console prints that the image property is changed for a thumbnail, but not in the properties panel.
This also happens in the app/live game.
I am not sure about other yielding functions, the task.wait() looks like it’s also broken when used in combination with rapid input events firing. Shouldn’t it throw an error, instead of breaking code and letting it run?
This is not an engine bug. This code is poorly designed and prone to race conditions. You should have used the category Scripting Support.
Your GetUserThumbnailAsync is a yielding function. Each thread created by MouseButton1Click is taking time to execute, and clicking too quickly results in the conditions you are seeing. I see your point about how your code should theoretically stop people from clicking too fast because it immediately increments count (variable), but overall there are many better ways your code could be designed to handle this.
I have provided code that I believe should fix your issues and is designed with better capabilities (although, I am not quite sure what the point of your code is to be honest). Please utilize Scripting Support next time. Consider adding a debounce if you are worried about users clicking too fast.
local players = game:GetService("Players")
--
local player_random = Random.new()
--
local playerslist = workspace:WaitForChild("PlayersList")
local joinbutton = workspace:WaitForChild("JoinButton")
--
local jb_surfacegui = joinbutton:WaitForChild("SurfaceGui")
local pl_surfacegui = playerslist:WaitForChild("SurfaceGui")
local frame_r = pl_surfacegui:WaitForChild("FrameRight")
local frame_l = pl_surfacegui:WaitForChild("FrameLeft")
local join_textbutton = jb_surfacegui:WaitForChild("TextButton")
--
local thumbnail_fallback_image = "rbxassetid://failedtoload_orsomerandomimage"
--
local function getvaliduserid()
local attempts = 0
local userid, username
--
repeat
local potential_userid = player_random:NextInteger(1, 999999999)
--
local isreal_userid, potential_username = pcall(players.GetNameFromUserIdAsync, players, potential_userid)
--
if isreal_userid then
userid = potential_userid
username = potential_username
end
--
task.wait()
until ( userid and username ) or attempts >= 5
--
if userid then
return userid, username
else
local acceptable_random_userid = player_random:NextInteger(1, 999999999)
--
warn(`Failed to find a valid userid, returning acceptable random substitute {acceptable_random_userid}.`)
--
return acceptable_random_userid, "UnknownUser"
end
end
local function addplayer(slot)
local random_userid, random_username = getvaliduserid()
--
local thumbnail_imagelabel = frame_l:FindFirstChild(string.format("PlayerThumbSlot%s", slot))
local player_textlabel = frame_r:FindFirstChild(string.format("PlayerLabel%s", slot))
--
if thumbnail_imagelabel then
local success, thumbnail_assetid, isloaded = pcall(players.GetUserThumbnailAsync, players, random_userid, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size100x100)
--
if success and thumbnail_assetid then
thumbnail_imagelabel.Image = thumbnail_assetid
else
thumbnail_imagelabel.Image = thumbnail_fallback_image
end
--
thumbnail_imagelabel.Visible = true
end
if player_textlabel then
player_textlabel.Text = string.format("Player %s (%s)", slot, random_username)
--
player_textlabel.Visible = true
end
end
--
local amount_of_players = 0
local max_players = 7
--
join_textbutton.MouseButton1Click:Connect(function()
if amount_of_players < max_players then
amount_of_players += 1
--
addplayer(amount_of_players)
end
end)