Attempt to index 'Value'

Hello I am working on a shop for my game but when I try to buy something I keep getting an error:
ServerScriptService.Main.ShopMain:41: attempt to index nil with ‘Value’
Script ‘Players.VoidPan_Dev.PlayerGui.MainGUI.Shop.Core’, Line 299

  1. What do you want to achieve? I want to have the shop remove the currency from the player.

  2. What is the issue? Include screenshots / videos if possible!
    I keep getting an error: ServerScriptService.Main.ShopMain:41: attempt to index nil with ‘Value’

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried changing the leaderstats. I’ve looked at the forums but couldn’t find anything that could help.

I’m also using remote functions.

Server Script:

game.ReplicatedStorage:WaitForChild("ShopEvents"):WaitForChild("PurchaseItem").OnServerInvoke = function(player,itemName)
	local coins = player.leaderstats.Coins
	local item = game.ServerStorage.Items:FindFirstChild(itemName):GetChildren()
	local itemPicked = item[math.random(#item)]
	print(typeof(itemPicked))
	print(itemPicked)
	if itemPicked and itemPicked:IsA("Tool") then
		-- if the item exists
		if game.ServerStorage.PlayerData[player.Name].Inventory:FindFirstChild(itemName) then
			if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= itemName then
				-- currently unequipped
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = itemName 
				return "Equipped"
			else
				game.ServerStorage.PlayerData[player.Name].Equipped.Value = ""
				return "UnEquipped"
			end
		end
		
		if coins.Value >= item.Price.Value then -- line of error
			coins.Value = coins.Value - item.Price.Value
			
			local itemValue = Instance.new("ObjectValue")
			itemValue.Name = itemName
			itemValue.Parent = game.ServerStorage.PlayerData[player.Name].Inventory
			
			return true
		else
			return "NotEnoughCash"
		end
	else
		return "NoItem"
	end
end

Client:

buyButton.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.ShopEvents.PurchaseItem:InvokeServer(selectedItem.Value)
	print(selectedItem.Value)
	if result == true then
		buyButton.BackgroundColor3 = Color3.fromRGB(42,170,30)
		buyButton.Text = "Purchase Success!"
		BuyNoise:Play()
		wait(.5)
		buyButton.Text = "Equip"
		buyButton.BackgroundColor3 = Color3.fromRGB(55,210,45)
	elseif result == "NotEnoughCash" then
		buyButton.BackgroundColor3 = Color3.fromRGB(204,20,31)
		buyButton.Text = "insufficient Funds"
		InsignificantFunds:Play()
		wait(0.5)
		buyButton.Text = "Buy"
		buyButton.BackgroundColor3 = Color3.fromRGB(42,170,30)
	elseif result == "Equipped" then
		equippedItem.Value = selectedItem.Value
		buyButton.BackgroundColor3 = Color3.fromRGB(42,170,30)
		EquipNoise:Play()
		buyButton.Text = "Equipped!"
		wait(0.5)
		buyButton.Text = "UnEquip"
		buyButton.BackgroundColor3 = Color3.fromRGB(55,210,45)
	elseif result == "UnEquipped" then
		equippedItem.Value = "" 
		buyButton.BackgroundColor3 = Color3.fromRGB(42,170,30)
		buyButton.Text = "UnEquipped"
		EquipNoise:Play()
		wait(.5)
		buyButton.Text = "Equip"
		buyButton.BackgroundColor3 = Color3.fromRGB(55,210,45)	
	end
				
end)
2 Likes

It is saying that this

selectedItem.Value

is failing because this

selectedItem

does not exist. Can you please provide more of the script so we can see what selectedItem is meant to be?

1 Like

I think @VoidPan_Dev Should provide the script of where the remotefunction is being invoked

2 Likes

SelecteItem.Value is the item that is being selected in the shop:

local availableTools = game.ReplicatedStorage:WaitForChild("ShopEvents"):WaitForChild("GetTools"):InvokeServer()
--local availableChars = game.ReplicatedStorage:WaitForChild("ShopEvents"):WaitForChild("GetChar"):InvokeServer()
local mainFrame = script.Parent:WaitForChild("MainFrame")
local miniframe= mainFrame:WaitForChild("miniframe")
local itemInformation = miniframe:WaitForChild("ItemInformation")
local infoFrame = itemInformation.InfoFrame
local selectedItem = itemInformation.SelectedItem
local equippedItem = itemInformation.EquippedItem
local numberOfItems = #availableTools

-- far down
		selectedItem.Value = availableTools[i][1]
		print(availableTools[i][1])
1 Like

Here,
You did this

local item = game.ServerStorage.Items:FindFirstChild(itemName):GetChildren()

And this

if coins.Value >= item.Price.Value then

Item is a table and not an item so price won’t be a valid member of that table I tried the deleting the one I posted before but it says wait before being able to delete :-:

3 Likes