Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't an ImageLabel render an image?

Asked by 3 years ago
Edited 3 years ago

So basically I'm trying to use one of Nasa's APIs to create a roblox game. The API obviously returns JSON and images get returned as URLs.

Here's my code, to test the API (Not the final code)

local HttpService = game:GetService("HttpService")

local URL = "https://api.nasa.gov/planetary/apod?api_key=my api key" -- Not giving out my API key

local response = HttpService:GetAsync(URL)

local data = HttpService:JSONDecode(response)

local screenGUI = game:GetService("StarterGui"):WaitForChild("APIData")
local imgLabel = screenGUI.Image

print(data["url"])
imgLabel.Image = data["url"]

The code gets executed fine with no errors or warnings, the ImageLabel shows a white square when rendering this image , even though it's Image property was changed. Why does this happen?

0
You are doing everything correctly, though, you aren't able to get off-site images. It's unfortunate, but you'll just have to upload the image to Roblox and get the image from there instead. Maximus1027 30 — 3y
0
@Maximus1027 Is there any way to bypass this or upload the image through a ROBLOX game? SpectacularPavlos 86 — 3y
0
No, in my experience there is no way to bypass this. And I believe the same applies to animations. xXKrixlatedXx 7 — 3y
0
Classic mistake. You aren't supposed to change the StarterGui, you change the PlayerGui in the local player. NGC4637 602 — 3y
0
bru NGC4637 602 — 3y

1 answer

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

If it is a local script, do this:

local HttpService = game:GetService("HttpService")
local URL = "https://api.nasa.gov/planetary/apod?api_key=my api key"
local response = HttpService:GetAsync(URL)
local data = HttpService:JSONDecode(response)
local screenGUI = game.Players.LocalPlayer.PlayerGui:WaitForChild("APIData")
local imgLabel = screenGUI.Image

print(data["url"])
imgLabel.Image = data["url"]

if it is a server script, you can add a RemoteEvent in ReplicatedStorage then do this:

local HttpService = game:GetService("HttpService")
local URL = "https://api.nasa.gov/planetary/apod?api_key=my api key"
local response = HttpService:GetAsync(URL)
local data = HttpService:JSONDecode(response)

game.Players.PlayerAdded:Connect(function(player)
    local image = data["url"]
    game.ReplicatedStorage.RemoteEvent:FireClient(player,image)
end)

and then a local script in StarterPlayerScripts that has this:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(image)
    local screenGUI = game.Players.LocalPlayer.PlayerGui:WaitForChild("APIData")
    local imgLabel = screenGUI.Image

    print(image)
    imgLabel.Image = image
end)
Ad

Answer this question