Hello. I am currently working on a game where you chop trees and sell your logs (very cliché). When you chop the tree, a log gets dropped on the ground. You can then pickup this log and it will go into a Custom Inventory. I was wondering, how would I make a system where the player can sell their logs and I could set the amount a log is worth.
I am not sure where to start. I am thinking that I will need to store all of the logs and their stats via a ModuleScript and then require it?? I don’t know. Any help is appreciated!
Setting the amount the logs are worth in a modulescript is a good start, I do that too for stats in my game. As for the sell system, you could try fiddling around with stuff and see if something starts working. Maybe store the money somewhere and make a script subtract from it after selling? Maybe have a central script handle player economy? See what works and what doesn’t.
You should use a dictionary for the player inventory and for the prices of the logs and other items. It could go something like this:
local PlayerInventory = { -- taking this as an example of player inventory
["Logs"] = 15,-- amounts of logs they own
["Diamonds"] = 5,
["Gold"] = 2,
}
local sellingPrices = {
["Logs"] = 10, -- $10 per log
["Diamonds"] = 100,
["Gold"] = 50,
}
--[[
Now that we have the amount of logs, gold and diamonds to find the price it is very simple.
quantity * price = total price
]] --
function getPriceOfLogs()
local amountOfLogs = PlayerInventory["Logs"]
local priceOfLogs = sellingPrices["Logs"]
local totalPrice = amountOfLogs * priceOfLogs
return totalPrice -- returns 15 * 10 = 150
end
--// Alternatively if you only want to get the price of a number of logs and not all the logs the player has you could do something like this:
function getPrice(item, quantity)
return sellingPrices[item] * quantity
end
local price = getPrice("Logs", 10) --gives price of 10 logs
print(price) -- outputs $10*10 = 100
local price = getPrice("Gold", 25) --gives price of 25 gold
print(price) -- outputs $50 * 25 = $1250
--[[
In case you want to find the value of the whole inventory it would go like this:
]]--
local totalPrice = 0
for itemName, quantity in pairs(PlayerInventory) do --// looping through players inventory
local price = sellingPrices[itemName] --// getting price of a single item
local itemsValue = price * quantity --// getting the total price of users' items
totalPrice = totalPrice + itemsValue --// adding it to the total value
end
print(totalPrice) -- outputs ($10 * 15) + ($100 * 5) + ($50 * 2) = $750