[solved] How to make loot luck math

so i was coding a loot drop system so i stuck in the luck math i tried doing:

if luck >= ex:30 and luck < ex:50 then

and it did not work please help

1 Like

post full script so people can help you out more

1 Like
local spawnLocations = {}
local config = require(script.Droppables.Config)

for _,v in pairs(workspace.Spawns:GetChildren()) do
	if v:IsA("BasePart") then
		table.insert(spawnLocations,v)
	end
end

local debris = workspace:FindFirstChild("Debris")

if not debris then
	debris = Instance.new("Folder",workspace)
	debris.Name = "Debris"
end

while task.wait(3) do
	warn("here")
	local RandomSpawn = spawnLocations[math.random(1,#spawnLocations)]
	local random = math.random(1,100)
	if random >= config.common then
		for _,loot in pairs(script.Droppables.Common:GetChildren()) do
			if loot:IsA("Tool") then
				loot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				loot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif random == config.legendary then
		for _,loot in pairs(script.Droppables.Legendary:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif random == config.uncommon then
		for _,loot in pairs(script.Droppables.UnCommon:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif random == config.rare then
		for _,loot in pairs(script.Droppables.UnCommon:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	end
end

Config Module:

local module = {
	common = 45,
	uncommon = 30,
	rare = 20,
	legendary = 5,
}

return module

i made changes to it and it not solved please help

I took a part where the main problem was. And made a luck system ig :>

Sorry if my code looks messy, I’m currently learning Lua and stuff.

function Luck(Config, SmallestChance)
	local _ = 1
	local Table = { Config }
	while true do
		if #Table == math.floor(Config / SmallestChance) then
			return Table
		end
		Table[#Table + 1] = Config
	end
end

function MergeTable(x, y) for k,v in pairs(x) do y[k] = v end end

local SmallLuck = config.legendary -- Put smallest luck here.

local LuckTable = {}
MergeTable(Luck(config.common, SmallLuck), LuckTable)
MergeTable(Luck(config.uncommon, SmallLuck), LuckTable)
MergeTable(Luck(config.rare, SmallLuck), LuckTable)
MergeTable(Luck(config.legendary, SmallLuck), LuckTable)


-- I renamed all config.<rarity> to LuckTable.<rarity> :3

while task.wait(3) do
	warn("here")
	local RandomSpawn = spawnLocations[math.random(1, #spawnLocations)]
	local Random = math.random(1, #LuckTable) -- Changed from math.random(1, 100)
	local Result = LuckTable[Random] -- New.

	if Result == LuckTable.common  then
		for _, loot in pairs(script.Droppables.Common:GetChildren()) do
			if loot:IsA("Tool") then
				loot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				loot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif Result == LuckTable.legendary then
		for _, loot in pairs(script.Droppables.Legendary:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif random == LuckTable.uncommon then
		for _, loot in pairs(script.Droppables.UnCommon:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	elseif random == LuckTable.rare then
		for _, loot in pairs(script.Droppables.UnCommon:GetChildren()) do
			if loot:IsA("Tool") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot.Handle.CFrame = RandomSpawn.CFrame
			elseif loot:IsA("Model") then
				local newloot = loot:Clone()
				newloot.Parent = debris
				newloot:PivotTo(RandomSpawn.CFrame)
			end
		end
	end
end

I’m not very good at explaining, so here is the best I got.

So I took the smallest chance of getting a rarity item which is the Legendary, and made a function that will divide itself with the legendary, example config.common / config.legendary returns 9, and then appends config.common 9 times into the LuckTable, and so on with other rarity

This is the created lucktable with your config

--[[
This is the created lucktable with your config.
config.common = 45 -- 9 times
config.uncommon = 30 -- 6 times
config.rare = 20 --  4 times
config.legendary = 5 -- 1 times
]]
LuckTable = {45, 45, 45, 45, 45, 45, 45, 45, 45, 30, 30, 30, 30, 30, 30, 20, 20, 20, 20, 5}

The comments explains.
Like for vouch :smiley:

2 Likes

it took hours but worth it for learning.

1 Like

Hello Thanks For Reply but i solved it myself!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.