#Image of Imagelabel doesn't change when changing it from scripts
207 messages · Page 1 of 1 (latest)
is the batteryLevels a string id?
** You are now Level 6! **
thats the id from the asset manager
Don't do that
Instead create a table
Wait
I've got all day
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
alr so rn you have something like that
local batteryImages = {
1000000,
2000000,
3000000,
}
local function ChangeBatteryLevel(battery)
assert(type(battery) == "number", "Expected number")
-- ...
end
alr
now instead of doing that you can use tables inside of tables
wait what?
thats what you have rn
yeah
the ... is your code
mhm
like if ... then elseif ... then ...
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)
mhm
you will see that it will display the keys ( 75, 50 ... ) and the values
lol no dont worry
Alright
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)
;compile
3000000
i have one to
and check if the battery is negative
to
yes but like add it here
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)
;compile
4000000
genious
it will set the max between 0 and the number
mhm
so it sets all negative nums to 0
and you can check for the battery after if you want performance
Mhm
however, even replacing it is just a matter of nanoseconds
Ill try now as i forgot to round the battery level
Huh
It still doesnt work, i think its because of the ids
yes haha
there
yes but here, its not switch haha
even here
do i put the ids in a string on leave them as a number?
Running rn
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
cause you need to change the id 😂
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
like that
wait dont tell me i shouldnt have done image.image
does your .Image is the p^roperty or the child
yes else you just get it
you cant set it
thats the property of the imagelabel right?
except if you get the class
well you cant set it like that
my script is allergic to working
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
this doesnt feel right
yep no it doesnt work
basically,
local image = imageLabel.Image here you GET the image but you cant set it
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?
what is BatteryLevelImage ?
show me the local BatteryLevelImage = ...
``` this but renamed
i just changed image to batterylevelimage
more readable
(for me)
do print(id, battery)
alr
huh
everytime i enable the flashlight my image dissapears
and it changes the image id correctly
hmmm
its maybe not the good id
im checking the ids
do
BatteryLevelImage.Image = "rbxassetid://" .. tostring(id)
thats weird
what happend when you do that ?
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?
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
** You are now Level 7! **
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
?
only prints 'test'
so it doesnt work ?
yeah no
god
but after is like its 3.30pm rn, ill test around 5.15pm
back
lets say it toggles when you press "R" it removes 10 to the battery level
i cant help more ill just do that i gig soon
alr
--!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
Sorry for late response
It's ok
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