How to make a chance of something happening when you touch a part

yes but if i want more results just incase

In order to have a more varied amount of results, with their own percentages, you would need to make sure all of the numbers add up to 100% in order to have accurate percentages (you could ignore this rule, but then the numbers would not be exactly how you intented them to be).

For example, if you wanted to add another item to the list, you would need to make sure you deplete the values of the other items in the table. Unless, of course, you don’t want to use percentages, and simply ignore the rule.

----------------

If you were to ignore the “add up to 100” rule, the only way thing you would know is the chance of the item relevant to the others. Here’s an example:

local ItemList = {
Gem = 20,
Participation = 90
}

Obviously, that adds up to 110, which would mean it is not exactly 20% and 100% (It is actually 20/110 and 90/110). BUT, it is obvious that it is more likely for the Participation item to be selected due to it being higher.

1 Like

alternatively you could do this.

local part = script.Parent
local lootTable = {
	["20 coins"] = {
		Chance = 10, 
		Amount = 20
	},
	["10 coins"] = {
		Chance = 100, 
		Amount = 10
	}
}
local function getRandomChance(input, returnValue)
		local function GetSum(Table)
			local Sum = 0
			for _, Data in pairs(Table) do
				Sum += Data.Chance
			end
			return Sum
		end
		local function Get(Table)
			local Rng = Random.new()
			local RandomNum = Rng:NextNumber(0, GetSum(input))
			for _, Data in pairs(Table) do
				if RandomNum <= Data.Chance then
					return Data
				else
					RandomNum -= Data.Chance
				end
			end

		end
		local Data = Get(input)
		if returnValue ~= nil then
			return Data[returnValue]
		else -- no return value
			return Data
		end
end
part.Touched:Connect(function(Hit)
	local hum = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if hum then
		local char = hum.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		if plr then
			plr.leaderstats.Coins.Value += getRandomChance(lootTable, "Amount")
		end
	end
end)

it’ll add coins every time you touch the part. Which will get a random amount with the chance given. You can play around with the chances and see that it does really work. You can add/remove tables from the lootTable. Just make sure each mini-table has an index called Amount and Chance
@Philipceo90

how does it work that you have 10 chance for 20 coins and 100 chance for 10 coins that would be intotal 110

No you got it all wrong. The chance index is the chance of that amount of coins being given. The amount is the amount added onto the player’s coins. Try it out

would making 2 chance variables work?

If you’re only looking for one end result, no. I highly recommend using the script that you sent the screenshot of, it looks very well done.

can you explain the for i = part?

Sure thing.

The two most commonly used numbers for for statements are 1 and the amount of intervals.

If you want the loop to run 5 times, you would do:

for i = 1, 5 do

end

The 1 is just the amount that i (the interval) increases by on each iteration. For the script you sent, it runs the loop however many times the chance for each item is. If the chance is 30, the loop will add the item to a table 30 times.

Then, it gets a random item from that table. Let’s say the table looks like this:

{Coin, Coin, Coin, Gem}

Chances are, the script will select Coin if it were random. There is still a 25% chance it will not.