Why ObjectValue can't actually get the object?

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??

i think you mean;

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

Read the title, I’m using an Object value, not StringValue

That’s why I put

Can you tell me the errors?

v.Name is a string, not an object

Yes, but then how are you supposed to find the child of the backpack? I just tested this part of the script

and got an error, so, proved my point(sorry if this sounds a bit rude, just trying to help)

attempt to call a table value

I don’t get any errors. The problem isn’t assigning the value, the problem is getting the value from the backpack.


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

I don’t really understand the problem, but try this:
Backpack[CurrentSlot.Item.Value]

bad argument #2 (string expected, got Object)

Instance:FindFirstChild(child) takes the name of the child, which is to be a string. Giving it an object will hence error.

may I ask what “CurrentSlot” is referencing?

CurrentSlot is an ImageButton. CurrentSlot.Item is the 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.

local ItemInBackpack = CurrentSlot.Item.Value
2 Likes

I think the problem is that the value of the “object” you are referencing is 1, so you are basically doing BackPack[1]

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
1 Like

Where do I set CloneHolder.Parent??

Edited script, it should be ItemInBackpack