Delete script not work, help

Hi, everyone. :объятий:
My script, which should delete the first item on the second opening of the case and leave a new item, does not work. Can you help me or tell me how to finish it correctly so that it works?

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

local RP =  game:GetService('ReplicatedStorage')
local ItemFolder = RP.ItemFolder

local function onClicked(player)

local ChosenItem = Label.Text
wait(0.5)
print(ChosenItem)
Label.Text = 'You got '.. ChosenItem

if ItemFolder:FindFirstChild(ChosenItem) then
	
	local delete = ItemFolder[ChosenItem]:Delete()
	delete.Parent = player.Backpack
	
	end
	
	end

this script should remove the old item and leave the one that recently appeared in the backpack. But he doesn’t want to work. Help please

Is this code inside the script that’s under the ClickDetector?

Yes, this code is located inside clickdetector

I need to delete the old item when clicked and keep the new one that was recently received

Because:

local CD = script.Parent.ClickDetector

The location of the ClickDetector is just script.Parent because this script is a child of ClickDetector.

local Label = script.Parent.BillboardGui.TextLabel

Same thing for this too, the TextLabel is located at script.Parent.Parent.BillBoardGui.TextLabel, not script.Parent.BillboardGui.TextLabel.

So I have to remove them? or did I get it wrong?

Replace :Delete() with :Destroy() as delete isn’t something that exists within Roblox lua.

2 Likes

I really didn’t notice this mistake. thanks

But even after correcting this error, my old item does not disappear, and does not change to a new one😔

You just have to switch script.Parent.ClickDetector with script.Parent and script.Parent.BillboardGui.TextLabel with script.Parent.Parent.BillBoardGui.TextLabel

1 Like

You didn’t properly reference your click detector and text label. You have to change it to

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

You also have to keep in mind that when you use :Destroy(), you’re getting rid of the object and can no longer refer to it. So instead of destroying the object, you should just change it to

local item = ItemFolder:FindFirstChild(ChosenItem)

if item then
    item.Parent = player.Backpack
end
1 Like

Thank you I’ll try to do it. I hope I can do it:)

Apart from the errors in your code (which the others are addressing), your click detector is inside a model.

I believe it is supposed to be inside a part, else it does not work.

I might be wrong though.

ClickDetectors do work if they are parented by a Model.
https://developer.roblox.com/en-us/api-reference/class/ClickDetector

Should’ve looked at the documentation a bit more, thanks.

So I need to move the script from clickdetector to the model?

You don’t need to, just do the changes we told you to do above and it should work fine.

Okey. I’ll try it later, thank you:)

I tried Your tips recently and they didn’t help, the old item still doesn’t disappear, I don’t know what the problem is, maybe I’m doing something wrong, because I don’t know much about scripts. Help ples

This is my modified script, according to your advice

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

local RP =  game:GetService('ReplicatedStorage')
local ItemFolder = RP.ItemFolder

local function onClicked(player)
	
	local ChosenItem = Label.Text
	wait(0.5)
	print(ChosenItem)
	Label.Text = 'You got '.. ChosenItem
	
	local item = ItemFolder:FindFirstChild(ChosenItem)
	
	if item then
		item.Parent = player.Backpack
	end
	
end
local CD = script.Parent
local Label = CD.Parent.BillboardGui.TextLabel

local RP =  game:GetService('ReplicatedStorage')
local ItemFolder = RP.ItemFolder


local OldItem = nil
local function onClicked(player)
	if OldItem ~= nil then
		OldItem:Destroy()
	end
	local ChosenItem = Label.Text
	wait(0.5)
	print(ChosenItem)
	Label.Text = 'You got '.. ChosenItem
	local item = ItemFolder:FindFirstChild(ChosenItem)
	if item then
		item.Parent = player.Backpack
		OldItem = item
	end
end
1 Like