Hey i’m looking at making a hover clone to a gui then disappear after the next button is clicked.
They won’t work after one button was selected
There are three issues with your code:
- Equipped will always be nil as you never change its value. This means your code will never destroy the previous sample as intended.
- When destroying the Sample, you are attempting to index a nonexistent Sample within the clicked button rather than the button in which the Sample exists
- The code to clone the Sample into the button will have to be run twice if a Sample were to exist
Fixed code:
local CurrentSample
for i,v in ipairs(script.Parent:GetChildren()) do
if v:IsA('TextButton') then
v.MouseButton1Click:Connect(function()
local Sample = game.ReplicatedStorage.Selected:Clone()
if CurrentSample then
CurrentSample:Destroy()
CurrentSample = nil
end
CurrentSample = game.ReplicatedStorage.Selected:Clone()
CurrentSample.Parent = v
end)
end
end
Also, next time please don’t paste images of your code as it makes it more inconvenient to help you.
1 Like
Now I could be wrong, but don’t you have to name your clone? The variable name that references it is called Sample, but when you call v.Sample:Destroy()
, you haven’t named it. In fact, your clone is probably called “Selected” from your ReplicatedStorage.
I haven’t worked with clones so I don’t know for sure, but this is what bulged out of your code from my perspective.
alright I wrote mines in a module script so it can shared to other objects instead of writing it out again.
local module = {}
function module.AddSelectedObject(dis)
local CurrentSample -- i dont want to use sample ok
for i,v in ipairs(dis:GetChildren()) do
if v:IsA('TextButton') then
v.MouseButton1Click:Connect(function()
local Sample = game.ReplicatedStorage.Selected:Clone()
if CurrentSample then
CurrentSample:Destroy()
CurrentSample = nil
end
CurrentSample = game.ReplicatedStorage.Selected:Clone()
CurrentSample.Parent = v
end)
end
for i,v in ipairs(dis:GetChildren()) do
if v:IsA('TextButton') then
v.MouseEnter:Connect(function()
v.BackgroundColor3 = Color3.fromRGB(235,235,235)
end)
v.MouseLeave:Connect(function()
v.BackgroundColor3 = Color3.fromRGB(255,255,255)
end)
end
end
end
end
return module
1 Like