Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

More efficient way to change image of gui on MouseEnter & MouseLeave?

Asked by 7 years ago

This works fine, I was just seeing if there is a better way to do something like this. Basically when you hover over the ImageButton it will use the 2nd value in the array to change the image of the ImageButton

local assets = {
591355902; --Non-Filled
591367709; --Filled
}

function fill()
    script.Parent.Image = "rbxassetid://"..assets[1]
end

function unfill()
    script.Parent.Image = "rbxassetid://"..assets[2]
end

function click()

end
script.Parent.MouseEnter:connect(fill)
script.Parent.MouseLeave:connect(unfill)

1 answer

Log in to vote
0
Answered by
tkcmdr 341 Moderation Voter
7 years ago

Hey YouSNICKER,

MouseLeave and MouseEntered are the only events you can connect to that will notify your script when their respective... events, occur. I would recommend cleaning things up a bit, however. If I may, I would like to suggest that you use something called a dictionary to store your image IDs. Consider the following:

local Icon = {
    Filled  = "rbxassetid://591367709";
    Unfilled    = "rbxassetid://591355902";
};

Then accessing them like so:

function fill()
    script.Parent.Image = Icon.Filled
end

function unfill()
    script.Parent.Image = Icon.Unfilled
end

I prefer Dictionaries over Lists by far, because a List position (i.e. Icon[2]) just doesn't have any meaning to someone who hasn't studied the code. This matters more when programs start to get really big (trust me, I would know.) Icon.Filled, however, does have more meaning and is much more memorable. As a rule, I generally avoid lists when dealing with data meant to be manually changed, so as to control the behavior of a game. Lists are a lot better when you're dealing with loads of unsorted data that you want to iterate over using a for loop, for example, but not for storing things that you need to remember.

I share this with you because it will help you a lot as you begin to work on more complex scripts; hopefully it helps you.

Have a good one, YouSNICKER, and best of luck with your project!

Yours truly, tkcmdr

0
Thanks man! YouSNICKER 131 — 7y
Ad

Answer this question