Hi! It has been around 2 weeks worth of my journey in Lua, and I’ve made system where a ‘Town’ has resources and has a stockpile of resources which can be increased/decreased via a GUI that pops up when you click the ‘Town’. Now, I’m thinking of making a lot of towns with different names but I realise this will occur a LOT of editing and remote events and I’ve also learnt about DRY (Don’t Repeat Yourself). So I have tried simplfying it with my limited knowledge and I want to see if it could be simplified even further. It would be great if someone were to advise me on this!
(There are also more resources which follow the same buy/sell system an the remotes, and if you are able to give me some advice on simplifying it, it will also be helpful!)
local RS = game:GetService("ReplicatedStorage")
local B = RS:WaitForChild("Buy")
local BLumberRemote = B:WaitForChild("BuyLumber") -- LumberRemote
-- IronRemote
-- SaltRemote
-- SpicesRemote
local S = RS:WaitForChild("Sell")
local SLumberRemote = S:WaitForChild("SellLumber") -- LumberRemote
-- IronRemote
-- SaltRemote
-- SpicesRemote
local SET = workspace:WaitForChild("Settlement") -- Folder
local L = SET:WaitForChild("LargeTown") -- Folder
local Name = L:WaitForChild("Malta") -- CHANGE TOWN NAME
local Town = Name:WaitForChild("TownBase") -- Where values are stored in
-- Town Values
local Prosperity = Town:WaitForChild("Prosperity") -- Prosperity
local Lumber = Town:WaitForChild("Lumber") -- Lumber Stock
-- Iron Stock
-- Salt Stock
-- Spices Stock
local debounce = false
-- BUY
BLumberRemote.OnServerEvent:Connect(function(plr) -- Lumber
if debounce == false then
debounce = true
local leaderstats = plr.leaderstats
local ducats = leaderstats:WaitForChild("Ducats") -- Ducats
local Inventory = plr:FindFirstChild("Inventory")
local lumber = Inventory:WaitForChild("Lumber")
local weight = plr:FindFirstChild("Weight")
if Lumber.Value > 0 then
if ducats.Value > 1.5 then
ducats.Value = ducats.Value - 1.5
lumber.Value = lumber.Value + 1
Lumber.Value = Lumber.Value - 1
weight.Value = weight.Value + 2
Prosperity.Value = Prosperity.Value + 2
end
end
wait(0.1)
debounce = false
end
end)
-- SELL
SLumberRemote.OnServerEvent:Connect(function(plr) -- Lumber
if debounce == false then
debounce = true
local leaderstats = plr.leaderstats
local ducats = leaderstats:WaitForChild("Ducats") -- Ducats
local Inventory = plr:FindFirstChild("Inventory")
local lumber = Inventory:WaitForChild("Lumber")
local weight = plr:FindFirstChild("Weight")
if lumber.Value > 0 then
ducats.Value = ducats.Value + 1
lumber.Value = lumber.Value - 1
weight.Value = weight.Value - 2
Lumber.Value = Lumber.Value + 1
Prosperity.Value = Prosperity.Value + 2
end
wait(0.1)
debounce = false
end
end)
(On a side note, I am currently learning tables and modulescripts and so I might be a little blur when it comes to tables… I may ask for clarification.)