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

[Solved]Why am I receiving the error: "bad argument #2 to '?' (string expected, got Object)"?

edit: Answer has been acquired, solution in the Answer below.

Hello!

I'm currently attempting to improve my shop purchase script to take the actions away from the client and put as much as I can on the server. I'm receiving the following error:

14:05:14.792 - ServerScriptStorage.EventHandler:26: bad argument #2 to '?' (string expected, got Object)

I've got a feeling the problem is how I've laid out my Remote Event script. I'll readily admit to not being confident in my Remote Event script work.

Here is my server scripts handling the Remote Event, Line 26 is where the error triggers:

--//Get Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//EventFolder
local Events = ReplicatedStorage:WaitForChild("Events")

--//Remote Events
local CoinsPurchase = Events:WaitForChild("CoinsPurchase")
local ItemOwned = Events:WaitForChild("ItemOwned")

 -- PURCHASE WITH COINS --
CoinsPurchase.OnServerEvent:Connect(function(Player, Value, Amount)
if Player.leaderstats.Coins.Value >= Amount then
	print('Server says player has enough Coins for the purchase.')
		Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - Amount 
        print(Value)
        Value.Value = true
    else
    print("Player does not have enough Coins for the purchase.")
end

end)

--CHECK IF ITEM OWNED --
ItemOwned.OnServerEvent:Connect(function(Player,Value)
if Player.Inventory[Value].Value == false then --This line is where the error triggers
	print('Player does not own this item.')
else
	print('Player does own item')
	end
	
end)

This is an abbreviated version of the Purchase Local Script, the player clicks the image for Item01, which prompts the BuyButton clickable button.

What I like for it to do after the player clicks the BuyButton is to check if the player owns the Item, right now its doing it locally first, and then firing the ItemOwned RemoteEvent (which is where the error triggers.) If they don't own the item, then it checks locally if the player has enough Coins, if so, it fires the CoinsPurchase RemoteEvent which turns the BoolValue to true, and deducts the amount. I find that when my script was all local/client-side doing the checking and coin removing, that it would occasionally mess up the purchase, or even send the players' coin total into the negative. Definitely can't have that.

BuyButton.MouseButton1Click:Connect(function()
MenuClickSound:Play()
	local amount = Cost
	local Value = ItemPurchased.Value --This is a StringValue with the Item's name on it.
	ItemDesc.Text = 'Purchase '..ItemTrackName.Value..'?'
	Price.Text = ItemPriceAmount.Value
		
	
	if Player.Inventory[Value].Value == true then
		ItemOwned:FireServer(Player.Inventory[Value]) --The RemoteEvent that triggers the error
		ItemDesc.Text = 'You have already purchased this Item!'
		wait(3)
	else
	

		if Player.leaderstats.Coins.Value >= amount then
			print('Item Own Test')
			ItemOwned:FireServer(Player.Inventory[Value])--The RemoteEvent that triggers the error
			print('Test 1')
			CoinsPurchase:FireServer(Player.Inventory[Value], amount)
			print('Test 2')
			wait(1)
			PurchaseClickSound:Play()
			ItemDesc.Text = 'Purchase Complete for '..ItemTrackName.Value..'! Equip this Item from your Inventory!'
			wait(3)		
			PurchaseFrame.Visible = false
			ItemDesc.Text = 'Purchase '..ItemTrackName.Value..'?'
			wait(1)
		else
			local coinsrequired = amount - currencyCOINS.Value		
			ItemDesc.Text = 'You need '.. coinsrequired ..' more Coins to buy this item!'
			wait(2)
			ItemDesc.Text = 'Purchase '..ItemTrackName.Value..'?'
			wait(5)
			PurchaseFrame.Visible = false
		end
	end
end)--Closes Purchase Frame

Item01.MouseButton1Click:Connect(function() 
PurchaseFrame.Visible =  true 
ItemTrackName.Value = 'Item01' 
ItemDesc.Text = 'Purchase Item01?'
 ItemPurchased.Value = 'Item01' 
Cost = 10 
ItemPriceAmount.Value =  Cost 
end)	

Here is the Print Outs showing the Prints working, and the line in the server script tripping it:

Item Own Test 14:05:14.792 - ServerScriptStorage.EventHandler:26: bad argument #2 to '?' (string expected, got Object)

14:05:14.792 - Stack Begin

14:05:14.793 - Script 'ServerScriptStorage.EventHandler', Line 26

14:05:14.793 - Stack End

Test 1

Server says player has enough Coins for the purchase.

Item01

Test 2

Thank you for your help! :)

2 comments

1

"Value" is probably a string value. A string value is not a string. You need to use .Value

0

Thank you for the response. :) It is a StringValue. I'm confused what you mean about using ".Value", aren't I already doing that, in Line 26 where the error is? if Player.Inventory[Value].Value == false then

1 answer

Solution acquired!

I turned Line 26 of the remote event script from:

if Player.Inventory[Value].Value == false then --This line is where the error triggers

to

if Value.Value == false then --This line is where the error triggers

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