I have some trouble with the chance system

Hello so i’ve been working on a simple one piece game and i had to do the chance system of the fruits but i’ve faced some trouble while making it

			["Common"] = 520,
			["Uncommon"] = 250,
			["Rare"] = 150,
			["Epic"] = 70,
			["Legendary"] = 10,
			["Mythical"] = 5
		}

		local TotalValue = 0
		for _, num in pairs(Weight) do
			TotalValue += num
		end

		local function Chance()
			local R = math.random(TotalValue)
			local ResetVal = TotalValue
			local Rarity

			for rarity, num in pairs(Weight) do
				ResetVal -= num
				if R >= ResetVal then
					Rarity = rarity
					break
				end
			end

			local RarityFolder = game.ReplicatedStorage.Fruits:FindFirstChild(Rarity)
			local ChosenValue = RarityFolder:GetChildren()[math.random(#RarityFolder:GetChildren())]
print(ChosenValue)
ChosenValue:Clone().Parent = player.Backpack
		end

		Chance()
1 Like

So from what you can see , instead of giving me one tool they give more

2 Likes

from what i understand youre trying to make certain items drop at a lower frequency.

An easier way to do this is to generate a number from 0 to 1000 that way common has a 50% chance to drop (approximately 50%) u can make the “1000” a larger number to shift the odds further making rarer items more rare. u fiddle with it.

Next you take that random number u generated and go down the list from common to mythical. first check is the random number greater than common? if yes then item is a common drop, if not then check the next value. repeat that check all the way down in order.

1 Like

Hello so idk if i really understood what you meant but this is the script i made from what i’ve read and well it didn’t work . Thx for the feedback anyways .

local RandomNumber = math.random(1,1000)

if RandomNumber >= 520 then
	print("Common")
elseif RandomNumber >= 250 and RandomNumber < 520 then
	print("Uncommon")
elseif RandomNumber >= 150 and RandomNumber < 250 then
	print("Rare")
		elseif RandomNumber >= 70 and RandomNumber < 150 then
print("Epic")
		elseif RandomNumber >= 10 and RandomNumber < 70 then
			print("Legendary")
		elseif RandomNumber >= 5 and RandomNumber < 10 then
			print("Mythical")
end
1 Like

exactly, you dont need the “and” part in any of the if statements though. because if the first if isnt true then its already known that the value is less than 520. same goes for all the other ones.

but you can leave it as is and it should function the same. its just very slightly less efficient

1 Like

also fix your indentation lol. make it easier to read

2 Likes

ah ok sorry if it isn’t easy to read because i’m someone who’s a bit messy but concerning the problem , is there like no way to solve it or what because as i stated before . The problem is that it gives 2 tools or more at once and the other method still don’t work . Is it becuz i use a normal script instead of local script ?

1 Like

Doesn’t seem like this is the right snippet. Send the code where you are calling the roll.
Also send the entire code block just in case.

2 Likes

well i did try using local script but still doesn’t work probably module script might work

1 Like

can i see the new chance function?

you should be using a normal script. a local script would only make the tool appear for the jndividual client. no one else would see the tool

1 Like

i will still use the same chance function but in module script hope it works . Also i might use both of them

1 Like

dont use both as it will make more duplicates. If you are using a module script then where are you calling the chance function?

yeah from what i see it is still duplicating . So any other method ?

no im asking where in any of youre existing scripts do you call the chance function? like can you show the code. because module scripts dont run on their own. you need to require them in a normal script

i did require the module script and called the function in the normal script

thats what i want to see. can you show me that

ok so here’s the module script (it’s the same script as you suggested me)

local Chance = {}

function Chance.Roll()
	local RandomNumber = math.random(1,1000)

	if RandomNumber >= 520 then
		print("Common")
	elseif RandomNumber >= 250 then
		print("Uncommon")
	elseif RandomNumber >= 150 then
		print("Rare")
	elseif RandomNumber >= 70 then
		print("Epic")
	elseif RandomNumber >= 10 then
		print("Legendary")
	elseif RandomNumber >= 5 then
		print("Mythical")
	end
end

return Chance

and in the normal script i did this


local chance_System = require(game.ServerStorage.modules["Chance System"])

chance_System.Roll()

few questions.

  1. Where is the code in the original post? the original code doesnt look like a module script.

  2. is that all the code in the normal script? If so then i have another question

3)where does that normal script come from? it seems like you spawn that script when the player uses the proximity prompt right?

ok excuse me so i wanted to mean that i added those lines
and if u want to see the normal script then here it is with the module . It is with proximity as u said so .


local FirstText = "Yo ! what's up . So if you don't know who i am . Well...I am the best fruit seller of the world"
local SecondText = "Don't worry you might get a good fruit :) but remember there is no refunds ! Do you wanna buy one random fruit ?"
local ThirdText = "Ok here you go my friend"
local FourthText = "Well it's unfortunate"

local chance_System = require(game.ServerStorage.modules["Chance System"])


character.ProximityPrompt.Triggered:Connect(function(player)
	local dialogueText = player.PlayerGui.DialogueGui.Frame.DialogueText
	dialogueText.Visible = true
dialogueText.Parent.Visible = true
	dialogueText.Parent.YesButton.Visible = false
	dialogueText.Parent.NoButton.Visible = false
	for i = 1, #FirstText do
		dialogueText.Text = FirstText:sub(1, i)
		wait(0.03)
	end

	wait(1)
	for i = 1, #SecondText do
		dialogueText.Text = SecondText:sub(1, i)
		wait(0.03)
	end
	wait(1)
	dialogueText.Parent.YesButton.Visible = true
	dialogueText.Parent.NoButton.Visible = true
	dialogueText.Parent.YesButton.MouseButton1Click:Connect(function()
		dialogueText.Parent.YesButton.Visible = false
		dialogueText.Parent.NoButton.Visible = false
		for i = 1, #ThirdText do
			dialogueText.Text = ThirdText:sub(1, i)
			wait(0.03)
		end
		chance_System.Roll()
		wait(1.25)
		dialogueText.Visible = false
		dialogueText.Parent.Visible = false
	end)
	dialogueText.Parent.NoButton.MouseButton1Click:Connect(function()
		dialogueText.Parent.YesButton.Visible = false
		dialogueText.Parent.NoButton.Visible = false
		for i = 1, #FourthText do
			dialogueText.Text = FourthText:sub(1, i)
			wait(0.03)
		end
		wait(1)
		dialogueText.Parent.Visible = false
		dialogueText.Visible = false
	end)
	
	end)