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
-
What do you want to achieve? I want to have the shop remove the currency from the player.
-
What is the issue? Include screenshots / videos if possible!
I keep getting an error: ServerScriptService.Main.ShopMain:41: attempt to index nil with ‘Value’ -
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)