Very simple script that has literally not worked

I’ve tried revising this script a countless amount of times and nothing works. Here is what I have, apologies if it’s messy or unnecessary complicated. No errors are being outputted. It looks like it’ll go in a loop, not sure if it will but even without the elseif part it doesn’t work.

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.ImageTransparency == 0.8 then
		script.Parent.Parent.ImageTransparency = 0
		
	elseif script.Parent.Parent.ImageTransparency == 0 then
		script.Parent.Parent.ImageTransparency = 0.8
			
		end
	end)

I would do a few thing to debug your script

  1. Try checking the parents to make sure it is really there
  2. Make sure the property is image transparency that you are looking for.
  3. If not then make sure the script is not disable or required properly if it is a module
  4. Play solo and check things out and see what is happening.

Hope this helps!

Decimal comparisons will seldom be accurate, try something like

-- (modified code to work without if statements just to make it shorter for demonstration)

local obj = script.Parent

local function onClicked()
     obj.ImageTransparency = obj.ImageTransparency >= 0.8  and 0 or 0.8
end

script.Parent.Activated:Connect(onClicked)

There’s inconsistencies and floating point errors while comparing and performing arithmetic operations on decimal numbers in Lua.

2 Likes

If you want a neat example of this, you can reproduce this in Studio as well. Insert an ImageLabel, select it, and then run these in the commandline:

game.Selection:Get()[1].ImageTransparency = 0.8
print(game.Selection:Get()[1].ImageTransparency)
Outputs: 0.80000001192093
print(game.Selection:Get()[1].ImageTransparency == 0.8)
Outputs: false

Ah, I got it now. Thank you guys.