local function Setup()
for i = 1, #Backpack:GetChildren() do
if Backpack:GetChildren()[i]:IsA('Model') then
for slot = 1, 5 do
local Item = CurrentSlots[slot].Item
if not Item.Value then
Item.Value = Backpack:GetChildren()[i]
break
end
end
end
end
end
if CurrentSlot.Item.Value then
print(CurrentSlot.Item.Value) -- prints Sword
local ItemInBackpack = Backpack:FindFirstChild(CurrentSlot.Item.Value)
print(ItemInBackpack) -- prints nil
if ItemInBackpack then
ItemInBackpack.Parent = Character
end
end
Not sure why an ObjectValue can’t get the actual model itself??
for i, v in pairs(BackPack:GetChildren()) do
for slot = 1, 5 do
local Item = CurrentSlots[Slots].Item
if not Item.Value then
Item.Value = BackPack[v.Name]
break
This is a guess because you didn’t give any errors
Can see, Item.Value == Sword (Sword being a model, NOT a string)
I’m trying to get the item from the backpack, to the character when they press a key. So to do that I need to do
if CurrentSlot.Item.Value then
print(CurrentSlot.Item.Value) -- prints Sword
local ItemInBackpack = Backpack:FindFirstChild(CurrentSlot.Item.Value)
print(ItemInBackpack) -- prints nil
if ItemInBackpack then
ItemInBackpack.Parent = Character
end
end
Which it is printing Sword. The problem lies here
local ItemInBackpack = Backpack:FindFirstChild(CurrentSlot.Item.Value)
I want to know why a model inside backpack is not being picked up using ObjectValue
There’s no point using FindFirstChild() to get the object once you already have a reference to it from your ObjectValue. Completely beats the point of using one.
Is there a way to then clone said item, as well as remove it from the charcter when need be?
--Put the cloned item into Character
if CurrentSlot.Item.Value then
local ItemInBackpack = CurrentSlot.Item.Value
if ItemInBackpack then
local ClonedItem = ItemInBackpack:Clone()
ClonedItem.Parent = Character
end
end
-- remove from character
local ItemInCharacter = CurrentSlot.Item.Value
if ItemInCharacter then
if ItemInCharacter.Parent == Character then
ItemInCharacter:Destroy()
end
end
As I just realised, having the item leave the backpack then come back constantly fires a backpack.ChildAdded event (which I use to check for new items coming into the backpack, so I can add them to the backpack ui) So is there a way to clone the item from backpack, put the clone in character, then when I need to, remove the clone from the player?
A simple solution would be to create another ObjectValue in ItemInBackpack and set its value to the cloned object:
--Put the cloned item into Character
if CurrentSlot.Item.Value then
local ItemInBackpack = CurrentSlot.Item.Value
if ItemInBackpack then
local CloneHolder = Instance.new("ObjectValue")
CloneHolder.Name = "CloneHolder"
local ClonedItem = ItemInBackpack:Clone()
CloneHolder.Value = ClonedItem
ClonedItem.Parent = Character
CloneHolder.Parent = ItemInBackpack
end
end
-- remove from character
local ItemInCharacter = CurrentSlot.Item.Value
if ItemInCharacter:FindFirstChild("CloneHolder") then
local CloneHolder = ItemInCharacter.CloneHolder
if CloneHolder.Value.Parent == Character then
CloneHolder.Value:Destroy()
CloneHolder:Destroy()
end
end