How do i set the image label to the flag of the country that the player is from

it works for me look at the video attached

how does it work for u, wait lemme check if theres italy flag

ahhhhh theres not the italy flag, that my bad, thank you for the help :pray:

1 Like

Just to note for everyone here:

The suggestions you’ve had so far have been pretty good, but I would recommend you reconsider using multiple individual images - you’d be better off using spritesheets. You can either create that yourself, or you could look online for a resource, e.g. this CC0 country flag spritesheet here.

This would require very few changes to your current code, but would reduce the amount of HTTP requests to load each individual image asset - not to mention that these images likely don’t have to be very big, so we’re wasting memory by throwing them into individual images.

The biggest change would be that, instead of setting the Image property, you’d set the ImageRectSize and ImageRectOffset of the ImageLabel - you’ll be able to find a few resources about this on the DevForum, I’m sure.

e.g. in the case of some of the examples here:

Note: This is example code - you’ll have to make some changes to make it function

-- in some ImageAssets.lua module
return {
  ImageId = 'rbxassetid://the/spritesheet/image/id',
  Flags = {
    UK = { ImageRectOffset = Vector2.new(0, 0), ImageRectSize = Vector2.new(15, 15) }

    -- ... todo: the other rect offsets + sizes from the image you've uploaded
  }
}

-- in some ui code
local FlagImageAssets = require(the.image.asset.module)

local LocalizationService = game:GetService('LocalizationService')
local countryCode = LocalizationService:GetCountryRegionForPlayerAsync(player)

local details = countryCode and FlagImageAssets.Flags[countryCode] or nil
if not details then
  -- couldn't find the flag, handle this case somehow
  return
end

someFlagImageLabel.ImageRectOffset = details.ImageRectOffset
someFlagImageLabel.ImageRectSize = details.ImageRectSize

2 Likes

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