How can I improve upon my sell script?

What can I do to make this script more neat? :smiley:

local function price()
	local RandomNumber1 = Random.new():NextNumber(3,5)
	local RandomNumber2 = Random.new():NextNumber(3,5)
	local RandomNumber3 = Random.new():NextNumber(3,5)
	local places = 2
	local number = RandomNumber1
	local mult = 10^places
	number = math.floor(number*mult)/mult
	local places = 2
	local number2 = RandomNumber2
	local mult = 10^places
	number2 = math.floor(number2*mult)/mult
	local places = 2
	local number3 = RandomNumber3
	local mult = 10^places
	number3 = math.floor(number3*mult)/mult
	script.Parent.Shop1.PriceVal.Value = number
	script.Parent.Shop2.PriceVal.Value = number2
	script.Parent.Shop3.PriceVal.Value = number3
	script.Parent.Shop1.Price.PlayerName.Text = "$"..number
	script.Parent.Shop2.Price.PlayerName.Text = "$"..number2
	script.Parent.Shop3.Price.PlayerName.Text = "$"..number3
end
db = false
local function Sell(Name, Price)
	if db == false then
		local gameStats = game.Players:FindFirstChild(Name).leaderstats
		if gameStats.Pumpkins.Value >=1 then
			db = true
			script.Parent.Sell:Play()
			gameStats.Coins.Value = gameStats.Coins.Value + Price
			gameStats.Pumpkins.Value = gameStats.Pumpkins.Value - 1
			wait(0.1)
			db = false
		else
			db = false
		end
	end
end
script.Parent.Shop1.ClickDetector.MouseClick:Connect(function(plr)
	Sell(plr.Name, script.Parent.Shop1.PriceVal.Value)
end)
script.Parent.Shop2.ClickDetector.MouseClick:Connect(function(plr)
	Sell(plr.Name, script.Parent.Shop1.PriceVal.Value)
end)
script.Parent.Shop3.ClickDetector.MouseClick:Connect(function(plr)
	Sell(plr.Name, script.Parent.Shop1.PriceVal.Value)
end)
price()
while true do
	wait(20)
	price()
end
1 Like
task.wait()

local parent = script.Parent
local Shops = {
    [1] = parent["Shop1"],
    [2] = parent["Shop2"],
    [3] = parent["Shop3"]
}
local function price()
    local Vals = {
        ["RandomNumbers"] = {
        [1] = Random.new():NextNumber(3,5);
        [2] = Random.new():NextNumber(3,5);
        [3] = Random.new():NextNumber(3,5)
        };
    
        ["numbers"] = {
            [1] = 0,
            [2] = 0,
            [3] = 0
        };
    }
   local places = 2
   local mult = 10^places
   
   for t, v in next, Vals.numbers do
       Vals.numbers[t] = Vals.RandomNumbers[t]
       Vals.numbers[t] = math.floor(Vals.RandomNumber[t] * mult)/mult
   end
   
   for i,v in pairs(Shops) do
       v.PriceVal.Value = Vals.RandomNumbers[i]
       v.Price.PlayerName.Text = "$" .. tostring(Vals.RandomNumbers[i])
    end
end

db = false
local function Sell(Name, Price)
   if db == false then
      local gameStats = game.Players:FindFirstChild(Name).leaderstats
      if gameStats.Pumpkins.Value >=1 then
         db = true
         script.Parent.Sell:Play()
         gameStats.Coins.Value = gameStats.Coins.Value + Price
         gameStats.Pumpkins.Value = gameStats.Pumpkins.Value - 1
         task.wait(0.1)
         db = false
      else
         db = false
      end
   end
end

for i,v in next, Shops do
    v.ClickDetector.MouseClick:Connect(function(plr)
        Sell(plr.Name, v.PriceVal.Value) -- Judging by the second arg in sell; you putting (Shop1), you can do that, I just put the current shop.
    end)
end

price()
while task.wait(20) do
   price()
end