Hello! I’m excited to be here. This is my first post!
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!
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)
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.