Make click detector change color of Image Label

I want to make it so when you pick up a battery it changes the color of the battery icon that can be seen on the screen showing how many batteries are left.

The problem is that I always manage to mess up the most simple of scripts. The battery gets destroyed as intended it plays the pick up sound but it just refuses to change the color of the image button.
This is the script:

local Battery = script.Parent
local Icon = game.StarterGui.BatteryHUD.Frame.B1
local Click = Battery.ClickDetector
local Sound = script.Parent.Pickup

Click.MouseClick:Connect(function()
	Icon.ImageColor3 = Color3.fromRGB(255, 255, 255)
	Sound:Play()
	Battery:Destroy()
end)


I tried to make the script also make the icon invisible to see if the script refuses to work with the GUI and it didn’t.
As always there were no threads on devforum about it and no errors in the output.

You’re trying to use StarterGui to change the Icon’s color which will not work.

Changing the StarterGui will not change the gui of the player who clicked the detector.

What you would need to do is fire an event to a script in the player’s gui, which will change the icon.

I already tried it and it didn’t work

I think this will work :

local Battery = script.Parent

local ClickDetector = Battery.ClickDetector
local Sound = Battery.PickUp

ClickDetector.MouseClick:Connect(function(Player)
	Player.PlayerGui.BatteryHUD.Frame.B1.ImageColor3 = Color3.fromRGB(255, 255, 255)
	Sound:Play()
	Battery:Destroy()
end)

Can you show the script where you tried that?

It’s long gone. I deleted it an hour ago trying to fix the problem. Sorry for that.

It worked, I had to make a few adjustments to the script but it worked!

local Battery = script.Parent

local ClickDetector = Battery.ClickDetector
local Sound = script.Parent.Pickup

ClickDetector.MouseClick:Connect(function(Player)
	Player.PlayerGui:FindFirstChild("BatteryHUD").Frame.B1.ImageColor3 = Color3.fromRGB(255, 255, 255)
	Sound:Play()
	Battery:Destroy()
end)

Thanks a lot.

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