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
You just have to switch script.Parent.ClickDetector with script.Parent and script.Parent.BillboardGui.TextLabel with script.Parent.Parent.BillBoardGui.TextLabel
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
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