Okay, so everything is being created in the inventory when I buy the item the first time, but the second time I want to buy the item, I just want it to add 1 to a value that's in the item, but I keep getting this:
14:40:02.823 - Players.rak553.PlayerGui.aShop.ShopFrame.BuyFrame.BuyButton.Buy:30: attempt to index field 'ItemAmount' (a nil value)
This is the broken line:
Inventory.name.ItemAmount.Value = Inventory.name.ItemAmount.Value + 1
That's the whole code, please help me!
local plr = game.Players.LocalPlayer local button = script.Parent local Item = Instance.new('Folder') local ItemType = Instance.new('StringValue') local ItemAmount = Instance.new('NumberValue') local moneyAmount = plr.Money.Amount.Value local Inventory = plr.Inventory button.MouseButton1Click:connect(function() local price = script.Parent.Price.Value local name = script.Parent.ItemName.Value local typeName = script.Parent.ItemType.Value if (moneyAmount >= price) then moneyAmount = moneyAmount - price if (not Inventory:FindFirstChild(name)) then --Create Item Item.Parent = Inventory Item.Name = name --Create Item Type and Set it ItemType.Parent = Item ItemType.Name = "ItemType" ItemType.Value = typeName --Create ItemAmount and Set it ItemAmount.Parent = Item ItemAmount.Name = "ItemAmount" ItemAmount.Value = 1 else Inventory.name.ItemAmount.Value = Inventory.name.ItemAmount.Value + 1 end end end)
This happens because name
refers to the Name
property of the instance. I'm not sure if the lowercase version is deprecated, but you should use Name
if you'll rename stuff.
The reason for this error is because properties have precedence over children.
For example, if you had an object named "BrickColor" inside of a part, when you do part.BrickColor
, it will not reference the object, but the property of the part. To fix your problem, you can use :FindFirstChild("name")
, or rename it. I strongly suggest you rename it to something like "ItemName", to prevent conflict.
I also suggest that you have the server handle the purchase, so that clients cannot manipulate the requirements, via remotes .