Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

"bad argument #2 to '?' (string expected, got Object)"?

I've been making this custom inventory, and its very close to being finished. Just one little error on line 2. Here's the script.

game.ReplicatedStorage.EquipItem.OnServerEvent:Connect(function(player, item)
	player.Character.Humanoid:EquipTool(player.Backpack[item])
end)

I've tried putting "player.Character.Humanoid:EquipTool(player.Backpack[item].Name)" but that still prints out the same error. I've asked help from my friend who knows more about RemoteEvents than me, but he was confused too. I've looked on similar errors that people had, but nothing helped. Could someone please explain this?

2 comments

0

Print your item name and check it. Check to make sure that there is a child of Backpack named Bilinear. If there is not a child named Bilinear, that's your problem. You would specify the argument to the item parameter as something else.

0

The "item" value is actually named something else than BiIinear. Not sure how it got this, but someone helped me fix it earlier.

2 answers

Myself i'm not good at scripting, but I think I see the error. In your square brackets try putting "tostring", tostring will probably convert you "Item" into a string.

game.ReplicatedStorage.EquipItem.OnServerEvent:Connect(function(player, item)
    player.Character.Humanoid:EquipTool(player.Backpack[tostring(item)])
end)

1 comment

0

In fact it does convert it into a string, but now its printing "BiIinear is not a valid member of Backpack" I don't even know how that happens, but there's always a solution.

If the item is an object it would be done like this since a string needs to be in the brackets. Putting .Name after the bracket is saying the same thing but gets the name of the item in player.Backpack.

game.ReplicatedStorage.EquipItem.OnServerEvent:Connect(function(player, item)
    player.Character.Humanoid:EquipTool(player.Backpack[item.Name])
end)

11 comments

0

It prints the same error as the other answer, "BiIinear is not a valid member of Backpack"

0

Is the tool equipped when the script runs

0

No. This script makes the player equip the tool

0

Run the game and then use the explorer to check the tool in the Backpack. Check that it is there and if it is, then check it is named "Bilinear"

0

I run the game, I go on Current: Server to check if its there, and it is, and the items name is "Testing Item"

0

That's why. The item you are sending through the event is not in their backpack

0

But that makes no sense... the "item" variable sent through the event fire is "local item = script.Parent.ItemNameValue.Value" and ItemNameValue is the items name in the backpack, I have no idea whats going on

0

How is the value of ItemNameValue decided. As in how does the script give it it's value

0

So you click an item in workspace, a tool that is a copy of the item gets cloned from Lighting into your Backpack, the tool has a localscript, and that localscript locates ItemNameValue and its value is determined by setting it to the tools name

0

Then it should work. I can't think of a reason why it wouldn't work

0

post a new question about this problem with the details we were talking about here

This discussion is preserved as it was. New answers, comments, and votes are closed.