The button color changes when playing on a computer, but not a mobile device

Hello! I’m excited to be here. This is my first post! :wave:

I’ve been having an odd problem with my gui. I have a button that’s supposed to change to Yellow when its clicked on, and when it’s clicked again, it’s supposed to change back to Grey. I wrote this script, and it works perfectly fine on a computer and on Studio’s mobile device emulator.

function ChickConColor(button)
   local but = (button)
   if but.ImageColor3 == Color3.fromRGB(230, 230, 230) then --This is gray
      but.ImageColor3 = Color3.fromRGB(255,255,0) --this is yellow
   else
      but.ImageColor3 = Color3.fromRGB(230, 230, 230) --This is gray
   end
end

ChickCon.d.MouseButton1Click:connect(function()
   ChickConColor(ChickCon.d)
end)

However, when its played on an actual mobile device, the button turns yellow the first time its clicked on, but when its clicked on again, it never goes back to being gray and stays on the color yellow.

I’m confused why this is happening. Any explanation? Thank you in advanced!

2 Likes

Welcome to the devforum, congrats on your first post.

Please format your code correctly by using three grave marks (``) before and after your code.

Example:
```
print(“Example”)
```
Makes

print("Example")

If I were you, I’d store a boolean value and then check if that’s true and if it is make it false and swap the color, and vice versa.

2 Likes

Okay, thank you! Sorry, I didn’t know how to block the code.

I will change it to a bool, but do you think there’s an explanation why it behaves this way only on mobile?

I have no clue why you’re having an issue with mobile devices screwing it up, but that being said, I would’ve just done:

ChickCon.d.MouseButton1Click:connect(function()
local but = (button)
if but.ImageColor3 == Color3.fromRGB(230, 230, 230) then --This is gray
but.ImageColor3 = Color3.fromRGB(255,255,0) --this is yellow
else
but.ImageColor3 = Color3.fromRGB(230, 230, 230) --This is gray
end
end)

I’m very doubtful that you passing it out to the function would cause the issue, but who knows. In my years I’ve seen dumber reasons as to why code didn’t work.

The reason I passed it to the function was because i have several buttons that have this function. (about 15 different buttons), so i could make my code shorter. It used to work when I had it the other way around (the way you showed). I should just rewrite it the old way then. Thank you!

It’s very weird why it only doesn’t work on mobile devices. It worked on the Studio mobile device emulator without problems.

This works fine for me on a published place on both computer and mobile. If it doesn’t work for you still, put some print statements into the code to see where the code is stopping.

I would do this:

function ChickConColor(button)
    print("Function Starting")
	local but = (button)
	if but.ImageColor3 == Color3.fromRGB(230, 230, 230) then --This is gray
        print("Image is grey, changing to yellow")
		but.ImageColor3 = Color3.fromRGB(255,255,0) --this is yellow
	else
        print("Image is not grey, changing to grey")
		but.ImageColor3 = Color3.fromRGB(230, 230, 230) --This is gray
	end
end

ChickCon.d.MouseButton1Click:connect(function()
    print("ButtonClicked")
	ChickConColor(ChickCon.d)
end)

Which ever bit is not printing, that is the bit that isn’t working.

As @RedDuck765 print statements are useful but in this case, you are using MouseButton1Click. Change MouseButton1Click to Activated. Here is the code!
MouseButton1Click will only fire on a mouse input where as .Activated should fire on both desktop and mobile.

function ChickConColor(button)
	if button.ImageColor3 == Color3.fromRGB(230, 230, 230) then --This is gray
		button.ImageColor3 = Color3.fromRGB(255,255,0) --this is yellow
	else
		button.ImageColor3 = Color3.fromRGB(230, 230, 230) --This is gray
	end
end

ChickCon.d.Activated:Connect(function()
	ChickConColor(ChickCon.d)
end)
3 Likes

Interestingly, although it should be done like this, it was still working when I tested MouseButton1Click on mobile… :man_shrugging:

In addition to what FracturedSoftware said, there is also InputBegan (mini-UIS specific to a GUI Object). You can check for UserInputType as either MouseButton1 or Touch.