How do I work with Destroy?

Hello:)
How do I make the old item disappear in Roblox Studio when opening the next case?
robloxapp-20200905-2103202.wmv (7.4 MB)

‘’’
–Put Your Item Name inside–

local ItemTable = {
‘Electro’,
‘Electro’,
‘SunGodPowers’,
‘SunGodPowers’
}

local CD = script.Parent.ClickDetector
local Label = script.Parent.BillboardGui.TextLabel

–Debounce Mechanism–

local Debounce = false
local Stop = false

–Put your Item inside a folder in Replicated Storage–

local RP = game:GetService(‘ReplicatedStorage’)
local ItemFolder = RP.ItemFolder

–Click Function–

local function onClicked(player)

--Checking--
print('Clicked')

--if debounce = true--
if Debounce then print('Debounce Active') return end

Debounce = true

--Stop = true if the function is running--
spawn(function()
	wait(5)
	Stop = true
end)

math.randomseed(tick())


--choosing 1 item every 0.1 seconds until stop = true--
repeat
	
	local ItemDisplay = ItemTable[math.random(1,#ItemTable)]
	Label.Text = ItemDisplay
	wait(0.1)
	
until Stop == true


--chosen item result--
local ChosenItem = Label.Text
wait(0.5)
print(ChosenItem)
Label.Text = 'You got '.. ChosenItem


--finding the item in the folder--
if ItemFolder:FindFirstChild(ChosenItem) then
	
	local cloned = ItemFolder[ChosenItem]:Clone()
	cloned.Parent = player.Backpack
	
end


--stop the debounce--
Stop = false
wait(1)
Debounce = false

end

–Run the function when you left click–
CD.MouseClick:Connect(onClicked)
‘’’

1 Like

What do you mean by old item? I’ll need more information so I can help you with your problem.

2 Likes

Yeah we will need more information to help even with regarding you previous posts that you have made. Specifically what you mean by “old item”

Edit: I see you also made this post below, Sorry I was planning to respond but the information was unclear especially with the script formatting.

I Think you mean When player Buys something from shop the old item disappear from it. Did i said right?

You can remove instances by calling :Destroy() on them. That will also disconnect any events associated with it and recursively destroy any descendants of that object. If you have any variables referencing the thing you’re destroying also make sure to set them to nil so that it is freed from memory.

1 Like

robloxapp-20200905-2103202.wmv (7.4 MB)

Here is an example of how to use :Destroy

local Brick = workspace:FindFirstChild("Brick") -- :FindFirstChild() means it will find the first part called Brick in the workspace and assign it as whatever you set the local name to

function onClicked() --functions when the part is clicked
	
	Brick:Destroy() -- self-explanatory
	
end

script.Parent.ClickDetector.MouseClick:connect(onClicked) --sets a name for the click function

If you put that script and a clickdetector inside of a part, then name something “Brick”, whenever you are in game and you click the part it will delete the object named Brick.

1 Like

That video definitely cleared up some information based on what you are wanting to do.

Judging by the script it seems to function by putting a tool into the players backpack.

If you want to get rid of the powers try adding a script on click of the case that will :Destroy() the characters tool or set its parent to nil.

1 Like

Thank you very much!! it helped me

1 Like

Tip with the dev forums, include as may videos or gifs as possible (use a screen recorder like ShareX for windows, Kap for mac) then upload onto imgur or streamable they are the easiest to understand a problem from. Moreover, use three backticks ``` to format code like such, doing both of these things will help you get a response much quicker by interested devs:

–Put Your Item Name inside–

local ItemTable = {
‘Electro’,
‘Electro’,
‘SunGodPowers’,
‘SunGodPowers’
}

local CD = script.Parent.ClickDetector
local Label = script.Parent.BillboardGui.TextLabel

–Debounce Mechanism–

local Debounce = false
local Stop = false

–Put your Item inside a folder in Replicated Storage–

local RP = game:GetService(‘ReplicatedStorage’)
local ItemFolder = RP.ItemFolder

–Click Function–

local function onClicked(player)


--Checking--
print('Clicked')

--if debounce = true--
if Debounce then print('Debounce Active') return end

Debounce = true

--Stop = true if the function is running--
spawn(function()
	wait(5)
	Stop = true
end)

math.randomseed(tick())


--choosing 1 item every 0.1 seconds until stop = true--
repeat
	
	local ItemDisplay = ItemTable[math.random(1,#ItemTable)]
	Label.Text = ItemDisplay
	wait(0.1)
	
until Stop == true


--chosen item result--
local ChosenItem = Label.Text
wait(0.5)
print(ChosenItem)
Label.Text = 'You got '.. ChosenItem


--finding the item in the folder--
if ItemFolder:FindFirstChild(ChosenItem) then
	
	local cloned = ItemFolder[ChosenItem]:Clone()
	cloned.Parent = player.Backpack
	
end


--stop the debounce--
Stop = false
wait(1)
Debounce = false


end

–Run the function when you left click–
CD.MouseClick:Connect(onClicked)

Okey, Simply I quite recently came on this forum:)