#Image of Imagelabel doesn't change when changing it from scripts

207 messages · Page 1 of 1 (latest)

lucid bane
#

Ive changed the image ids when its in runtime it just doesnt change it for some reason.

#

Yes im calling the function

opal raven
lucid bane
heavy bloomBOT
#

studio** You are now Level 6! **studio

lucid bane
#

thats the id from the asset manager

lucid bane
#

wait heh

#

What do i do instead?

lucid bane
#

I've got all day

turbid cape
#

alr back

#

on computer

#

so you want to diosplay the good image depending on the battery level thats right ?

#

and you dont want to display the same image cause its useless

lucid bane
#

yep

#

mhm

#

peformace reasons

#

although i dont have a peformance issue

turbid cape
#

alr so rn you have something like that

local batteryImages = {
    1000000,
    2000000,
    3000000,
}

local function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    -- ...
end
turbid cape
#

now instead of doing that you can use tables inside of tables

lucid bane
#

wait what?

turbid cape
#

thats what you have rn

lucid bane
#

yeah

lucid bane
#

mhm

turbid cape
#

like if ... then elseif ... then ...

lucid bane
#

yep

#

do i just add that line?

turbid cape
#

no

#

now imagine you have this type of code

local batteryImages = {
    [75] = 1000000,
    [50] = 2000000,
    [25] = 3000000,
}

local function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    for key, value in next, batteryImages do
        print(key, value)
    end
end

ChangeBatteryLevel(33)
lucid bane
#

mhm

turbid cape
#

you will see that it will display the keys ( 75, 50 ... ) and the values

lucid bane
#

im actually stupid

#

why didnt i think of that

turbid cape
#

lol no dont worry

lucid bane
#

Alright

turbid cape
#

you can loop throigh the keys and check if the key is the best like that

#
local batteryImages = {
    [75] = 1000000,
    [50] = 2000000,
    [25] = 3000000,
}

local function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    local id = nil -- Promise of a number
    for key, value in next, batteryImages do
        if key > battery then break end
        id = value
    end
    print(id)
end

ChangeBatteryLevel(33)
ocean girderBOT
#
Program Output
3000000

turbid cape
#

which is the 25%

#

just make sure you have a [0] case

lucid bane
#

i have one to

turbid cape
#

and check if the battery is negative

lucid bane
#

to

turbid cape
#

yes but like add it here

lucid bane
#

im clamping it

#

and rounding it

#

so no negative numbers

#

and no decimals

turbid cape
#

or

#

better

#
local batteryImages = {
    [75] = 1000000,
    [50] = 2000000,
    [25] = 3000000,
    [0] = 4000000
}

local function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    battery = math.max(battery, 0)
    local id = nil -- Promise of a number
    for key, value in next, batteryImages do
        if key > battery then break end
        id = value
    end
    print(id)
end

ChangeBatteryLevel(-33)
ocean girderBOT
#
Program Output
4000000

lucid bane
#

genious

turbid cape
#

it will set the max between 0 and the number

lucid bane
#

mhm

turbid cape
#

so it sets all negative nums to 0

#

and you can check for the battery after if you want performance

lucid bane
#

Mhm

turbid cape
#

however, even replacing it is just a matter of nanoseconds

lucid bane
#

Ill try now as i forgot to round the battery level

#

Huh

#

It still doesnt work, i think its because of the ids

turbid cape
#

yes haha

lucid bane
#

from where do i get the ids

#

because no matter which i try it still fails

turbid cape
#
  [75] = <id>
#

you can use your old ids

turbid cape
lucid bane
#

Oh i forgot to change the table...

#

Im an idiot

#

Afk 2 min

turbid cape
#

however, your method is indeed fatser

#

but ugly

#

but faster

lucid bane
#

back

#

yeah there is no switch statement :/

turbid cape
#

even here

lucid bane
#

do i put the ids in a string on leave them as a number?

turbid cape
#

idk tbh

#

if it worked with string, use strings

lucid bane
#

if it even worked T-T

#

i used numbers using the editor

turbid cape
#

alr

#

tell me if it worked

lucid bane
#

Running rn

turbid cape
#

and send the error logs if you want me to help u

#

with the code

lucid bane
#

alr

#

it still doesnt work

turbid cape
#

show code + error log

#

( if any )

lucid bane
#

no error

#

it should be this

#

Here is the code

#
local cam = workspace.CurrentCamera
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Flashlight = RS.Flashlight
local Clone = Flashlight:Clone()
Clone.Parent = script.Parent

local Brightness = 3
local TS = game:GetService("TweenService")
local battery = 100
local drainRate = 0.0025
local Keybind = Enum.KeyCode.F
local UIS = game:GetService("UserInputService")
local Toggle = false
local Mouse = game.Players.LocalPlayer:GetMouse()
local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)
local batteryLevelImage = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("PlungeUI"):WaitForChild("Battery").Image
local batteryLevels = {
    [100] = 15657584407,
    [75] = 15657584517,
    [50] = 15657584640,
    [25] = 15657584759,
    [0] = 15657584941
}


UIS.InputBegan:Connect(function(Input, p)
    if p then return end
    if Input.KeyCode == Keybind then
        Toggle = not Toggle
    end
end)

RunService.RenderStepped:Connect(function()
    if Clone then
        Clone.Position = cam.CFrame.Position
        TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, Mouse.Hit.Position)}):Play()

        if Toggle and battery > 0 then
            TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
            battery = math.max(battery - time() * drainRate)
            ChangeBatteryLevel(battery)
        else
            TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
        end

    end
end)


function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    battery = math.round(math.max(battery, 0))
    local id = nil -- Promise of a number
    for key, value in next, batteryLevels do
        if key > battery then break end
        id = value
    end
    print(battery)
end
#

everything works besides the ChangeBatteryLevel func

turbid cape
lucid bane
#

i changed the ids?

#

i use these ones

turbid cape
#
local image = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("PlungeUI"):WaitForChild("Battery")

function ChangeBatteryLevel(battery)
    assert(type(battery) == "number", "Expected number")
    battery = math.round(math.max(battery, 0))
    local id = nil -- Promise of a number
    for key, value in next, batteryLevels do
        if key > battery then break end
        id = value
    end
    image = id
end
lucid bane
#

wait dont tell me i shouldnt have done image.image

turbid cape
#

does your .Image is the p^roperty or the child

turbid cape
#

you cant set it

lucid bane
#

thats the property of the imagelabel right?

turbid cape
turbid cape
lucid bane
#

my script is allergic to working

turbid cape
#
local imageLabel = script.Parent
local image = imageLabel.Image

image = "23456789" -- Doesnt change the image
imageLabel.Image = "23456789" -- Changes
#

since you dont change the variable

#

but the propperty

lucid bane
#

this doesnt feel right

turbid cape
#

¯_(ツ)_/¯

#

its pretty logic

lucid bane
#

yep no it doesnt work

turbid cape
#

basically,
local image = imageLabel.Image here you GET the image but you cant set it

lucid bane
#
    assert(type(battery) == "number", "Expected number")
    battery = math.round(math.max(battery, 0))
    local id = nil -- Promise of a number
    for key, value in next, batteryLevels do
        if key > battery then break end
        id = value
    end
    BatteryLevelImage.Image = id
    print(battery)
end
``` what am i doing wrong here?
turbid cape
#

show me the local BatteryLevelImage = ...

lucid bane
#
``` this but renamed
#

i just changed image to batterylevelimage

#

more readable

#

(for me)

lucid bane
#

huh

#

everytime i enable the flashlight my image dissapears

#

and it changes the image id correctly

#

hmmm

turbid cape
#

its maybe not the good id

lucid bane
#

im checking the ids

turbid cape
#

do

  BatteryLevelImage.Image = "rbxassetid://" .. tostring(id)
lucid bane
#

i changed all the ids to the asset ids

#

nope

turbid cape
#

thats weird

turbid cape
lucid bane
#

it did everything correctly

#

ill show output 1 sec

#

and my battery instantly goes to the empty one

#

when i enable the flashlight and it stays that way

#

no wait

#

(ignore that it isnt linear, ill fix it sometime)

#

@turbid cape can you make a really quick example that i can copy over to see if it works?

turbid cape
#

i cant

#

i don t have roblox studio on my device rn :/

lucid bane
#

oh

#

should i try other links as i think that the ids are right but its just ^ that part that i dont think it works

#

It half works

#

the problem is

#

as soon as i turn on the flashlight it goes to 50% (image)

#

and then after 75% it goes to 0% (image)

#

Its very strange that it does this

heavy bloomBOT
#

studio** You are now Level 7! **studio

lucid bane
#

im going to try my previous setup

#
    assert(type(battery) == "number", "Expected number")
    battery = math.round(math.max(battery, 0))
    local id = nil -- Promise of a number
    for key, value in next, batteryLevels do
        if key > battery then break end
        id = value
    end
    BatteryLevelImage.Image = "rbxassetid://" .. tostring(id)
    print(id, battery)
end
#
    [100] = 15657584407,
    [75] = 15657584517,
    [50] = 15657584640,
    [25] = 15657584759,
    [0] = 15657584941
}```
#

i hate this

#

i might have debugged it

#

@turbid cape i found the problem

turbid cape
#

yeppee

#

what was it

lucid bane
#

it doesnt go though the if

#

elseif

turbid cape
#

?

lucid bane
#

only prints 'test'

turbid cape
#

so it doesnt work ?

lucid bane
#

yeah no

turbid cape
#

lol

#

ill try after

lucid bane
#

god

turbid cape
#

but after is like its 3.30pm rn, ill test around 5.15pm

lucid bane
#

alr (my timezone too)

#

just do it in dms

turbid cape
#

back

turbid cape
#

i cant help more ill just do that i gig soon

lucid bane
#

alr

turbid cape
#
--!strict

local UserInput = game:GetService("UserInputService")

local gui = script.Parent
local imageLabel = assert(gui:FindFirstChildOfClass("ImageLabel"))

local keyCode = Enum.KeyCode.R
local charge = 100

local batteryImages = {
    [0] = "rbxassetid://15657584941",
    [25] = "rbxassetid://15657584759",
    [50] = "rbxassetid://15657584640",
    [75] = "rbxassetid://15657584517",
    [100] = "rbxassetid://15657584407",
}

local function UpdateBattery(battery)
    assert(type(battery) == "number")
    battery = math.max(0, math.min(100, battery))
    local result = nil
    for i = 0, 100, 25 do
        print(i)
        result = batteryImages[i]
        if i >= battery then break end
    end
    print(result, battery)
    imageLabel.Image = result
end

local function OnInput(input, isTyping)
    if isTyping then return end
    if input.KeyCode ~= keyCode then return end
    charge -= 10
    UpdateBattery(charge)
end

UserInput.InputBegan:Connect(OnInput)
UpdateBattery(charge)```
#

press r

#

it work fine

#

npow i gtg rn

#

bye

lucid bane
#

Shouldve pinged me

#

But alr

lucid bane
#

Sorry for late response

turbid cape
#

It's ok

lucid bane
#

YOOO bro thanks so much

#
local RS = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Flashlight = RS.Flashlight
local Clone = Flashlight:Clone()
Clone.Parent = script.Parent
local gui = script.Parent
local Brightness = 3
local TS = game:GetService("TweenService")
local battery = 100
local drainRate = 0.0075
local Keybind = Enum.KeyCode.F
local UIS = game:GetService("UserInputService")
local Toggle = false
local Mouse = game.Players.LocalPlayer:GetMouse()
local TI = TweenInfo.new(.1, Enum.EasingStyle.Sine)
local imageLabel = assert(gui:FindFirstChildOfClass("ImageLabel"))
local batteryImages = {
    [0] = "rbxassetid://15657584941",
    [25] = "rbxassetid://15657584759",
    [50] = "rbxassetid://15657584640",
    [75] = "rbxassetid://15657584517",
    [100] = "rbxassetid://15657584407",
}


UIS.InputBegan:Connect(function(Input, p)
    if p then return end
    if Input.KeyCode == Keybind then
        Toggle = not Toggle
    end
end)

RunService.RenderStepped:Connect(function()
    if Clone then
        Clone.Position = cam.CFrame.Position
        TS:Create(Clone, TI, {CFrame = CFrame.lookAt(Clone.Position, Mouse.Hit.Position)}):Play()

        if Toggle and battery > 0 then
            TS:Create(Clone.SpotLight, TI, {Brightness = Brightness}):Play()
            battery = math.max(battery - drainRate)
            UpdateBattery(battery)
            print(math.round(battery))
        else
            TS:Create(Clone.SpotLight, TI, {Brightness = 0}):Play()
        end

    end
end)


function UpdateBattery(battery)
    assert(type(battery) == "number")
    local result = nil
    for i = 0, 100, 25 do
        print(i)
        result = batteryImages[i]
        if i >= battery then break end
    end
    print(result, battery)
    imageLabel.Image = result
end```
#

ignore

#

i swear

#

everything i try is broken

#

now my flashlight is broken

#

oh nvm

#

i have to split this into 2 scripts

#

may god allow this to work