I am currently making a slot selection system and I am not sure how to “Unselect” a previously selected slot?
By “Unselect” I mean changing the last selected obj color back to the normal color.
I have tried a couple of methods like getting the first “obj”, and a last “obj” in a variable, then to be able to change the “last” one back into the normal color again but I didn’t seem to get it working for some reason.
The main frame:
Module:
local Module = {}
local Client = script.Parent.Parent.Parent.Client
function Module:SelectUpgrade(obj)
local normalColor = Color3.fromRGB(255, 255, 255)
local selectColor = Color3.fromRGB(0, 255, 0)
local selectedObj = tostring(obj)
return function()
Client.ScrollingFrame[selectedObj].ImageColor3 = selectColor
end
end
Client.ScrollingFrame.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Down:Connect(Module:SelectUpgrade(child))
end
end)
return Module
What I tried to do, but did not work:
local Module = {}
local Client = script.Parent.Parent.Parent.Client
local lastSelected
function Module:UnselectPrevious(obj)
local normalColor = Color3.fromRGB(255, 255, 255)
if obj then
Client.ScrollingFrame[obj].ImageColor3 = normalColor
end
end
function Module:SelectUpgrade(obj)
local selectColor = Color3.fromRGB(0, 255, 0)
local selectedObj = tostring(obj)
lastSelected = selectedObj
return function()
Client.ScrollingFrame[selectedObj].ImageColor3 = selectColor
if selectedObj ~= lastSelected then
self:UnselectPrevious(lastSelected)
end
end
end
Client.ScrollingFrame.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Down:Connect(Module:SelectUpgrade(child))
end
end)
return Module
You can divorce the visual effect from the actual selection functionality. Whenever a button is selected, internally you register this as the selected item, then run an update across the buttons that updates their colour based on if it matches the selected item or not.
Having many items may make looping non-viable though, so in that case you may want to consider addressing your current implementation. Where does your implementation fall off working?
The feature only works on the ImageButton to the bottom right and I do not have a clue why it is only working on that ImageButton.
Most likely something to do with:
lastSelected = selectedObj
Edit:
I added a print to lastSelected underneath the “return function()” to see which object is was last selected every time I select one and it only prints the one to the bottom right. (which is the only one that works)
It only prints the bottom right because you are setting the lastSelected variable when you connect the click event, not when the event actually gets called. Since the bottom right object is the last object that got added, lastSelected will always be this object.
I figured it out myself somehow. I would love feedback on this solution. Tell me if it was alright to do it like this or not, or maybe you might have a better solution?
Code:
local Module = {}
local Client = script.Parent.Parent.Parent.Client
local timesSelected = 0
local lastSelected
function Module:UnselectPrevious(obj)
local normalColor = Color3.fromRGB(255, 255, 255)
if obj then
Client.ScrollingFrame[obj].ImageColor3 = normalColor
end
end
function Module:Select(obj)
local selectColor = Color3.fromRGB(0, 255, 0)
return function()
local selectedObj = tostring(obj)
timesSelected = timesSelected + 1
if timesSelected % 2 == 0 then
Client.ScrollingFrame[selectedObj].ImageColor3 = selectColor
self:UnselectPrevious(lastSelected)
lastSelected = selectedObj
else
Client.ScrollingFrame[selectedObj].ImageColor3 = selectColor
if lastSelected ~= selectedObj then
self:UnselectPrevious(lastSelected)
lastSelected = selectedObj
end
end
end
end
Client.ScrollingFrame.ChildAdded:Connect(function(child)
if child:IsA("ImageButton") then
child.MouseButton1Down:Connect(Module:Select(child))
end
end)
return Module