I am trying to make it so that every time you click the button, the username and the image change. But after the code i put in, only the username changes after I put in the code, the image only changes the first time you click the button, please help.
local Players = game:GetService("Players")
local GetNameFromUserIdAsync = Players.GetNameFromUserIdAsync
local function randomUser()
local randomId = math.random(1, 2000000000)
local success, res = pcall(GetNameFromUserIdAsync, Players, randomId)
local image = script.Parent.Frame.UserFrame.PlayerImage
local thumbType = Enum.ThumbnailType.AvatarThumbnail
local thumbSize = Enum.ThumbnailSize.Size352x352
local content, isReady = Players:GetUserThumbnailAsync(randomId, thumbType, thumbSize)
local button = script.Parent.Frame.RollPlayer
local PLACEHOLDER_IMAGE = "rbxassetid://0"
button.MouseButton1Click:Connect(function()
image.Image = (isReady and content) or PLACEHOLDER_IMAGE
image.Size = UDim2.new(0, 420, 0, 420)
end)
if success then
return res
else
return randomUser()
end
end
print(randomUser())
local button = script.Parent.Frame.RollPlayer
local imageLabel = script.Parent.Frame.UserFrame.PlayerImage
button.MouseButton1Click:Connect(function()
script.Parent.Frame.UserFrame.Username.Text = randomUser()
end)
local Players = game:GetService("Players")
local GetNameFromUserIdAsync = Players.GetNameFromUserIdAsync
local function randomUser()
local randomId = math.random(1, 2000000000)
local success, res = pcall(GetNameFromUserIdAsync, Players, randomId)
local image = script.Parent.Frame.UserFrame.PlayerImage
local thumbType = Enum.ThumbnailType.AvatarThumbnail
local thumbSize = Enum.ThumbnailSize.Size352x352
local content, isReady = Players:GetUserThumbnailAsync(randomId, thumbType, thumbSize)
local button = script.Parent.Frame.RollPlayer
local PLACEHOLDER_IMAGE = "rbxassetid://0"
button.MouseButton1Click:Connect(function()
image.Image = (isReady and content)
or PLACEHOLDER_IMAGE
print(isReady and content)
image.Size = UDim2.new(0, 420, 0, 420)
end)
if success then
return res
else
return randomUser()
end
end
print(randomUser())
local button = script.Parent.Frame.RollPlayer
local imageLabel = script.Parent.Frame.UserFrame.PlayerImage
print(isReady and content)
button.MouseButton1Click:Connect(function()
script.Parent.Frame.UserFrame.Username.Text = randomUser()
end)
By calling the randomUser function you’re adding another connection to the MouseButton1Click event, and that is one of the main problems because you’re never disconnecting the event, which adds multiple connections that are trying to set multiple images at the same time.
You should probably do something like organize all the info you need for the random user, like storing it in a table.
I’m not sure what your goal is with the sizing of the ImageLabel, but I saw you use offset for the image size, but I recommend you use scale instead so you can get the right size for all screens, you can achieve this by either getting an auto-scale plugin to convert it to scale, or just change the UDim2 size to 0.1, 0, 0.1, 0 and then dragging to resize the ImageLabel to the preferred dimensions.
I’ve done #1 and #2 for you, here is the script…
local Players = game:GetService("Players")
local GetNameFromUserIdAsync = Players.GetNameFromUserIdAsync
local function randomUser()
local randomId = math.random(1, 2000000000)
local success, res = pcall(GetNameFromUserIdAsync, Players, randomId)
local thumbType = Enum.ThumbnailType.AvatarThumbnail
local thumbSize = Enum.ThumbnailSize.Size352x352
local content, isReady = Players:GetUserThumbnailAsync(randomId, thumbType, thumbSize)
local button = script.Parent.Frame.RollPlayer
local PLACEHOLDER_IMAGE = "rbxasset://textures/ui/GuiImagePlaceholder.png"
if success then
return {
ImageSize = UDim2.new(0,420,0,420),
Image = (isReady and content) or PLACEHOLDER_IMAGE,
Name = res
}
else
return randomUser()
end
end
print(randomUser())
local canGenerate: boolean = false
local button = script.Parent.Frame.RollPlayer
local image = script.Parent.Frame.UserFrame.PlayerImage
button.MouseButton1Click:Connect(function()
if canGenerate == true then return end; canGenerate = true
local userInfo = randomUser()
if userInfo then
image.Image = userInfo.Image
image.Size = userInfo.ImageSize
script.Parent.Frame.UserFrame.Username.Text = userInfo.Name
end
task.wait(2) -- recommended to add cooldown, can change or remove
canGenerate = false
end)