local chance = math.random(1,5)
if chance <= 3 then
1-3 represents 60% of 1-5.
local chance = math.random(1,5)
if chance <= 3 then
1-3 represents 60% of 1-5.
so only the math.random(1,100) strat would work?
also if i want more do i just add another chance variable?
I recommend you use my module here. You can get random chances with it. Once installed, move it into replicated storage and write this SERVER script. Put the script in server script service.
local module = require(game.ReplicatedStorage.RandomLibary) -- gets the module
local input = {
["20 coins"] = {
["Chance"] = 70, -- each input table must have a chance index
["Coins"] = 20 -- optional but this will be the amount of coins the player gets once landed.
},
["10 coins"] = {
["Chance"] = 2, -- each input table must have a chance index
["Coins"] = 20
}
}
local function plrAdded(plr)
local result = module:GetRandomChance(input, "Coins") -- gets a random chance.
if result then
plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value += result -- Make sure to change the path of it to where the coins are located in the player. This adds the amount of coins they got from the random chance.
end
end
game:GetService("Players").PlayerAdded:Connect(plrAdded)
u may notice how you get 10 coins less than when you get 20 coins.
i am not that good of a scripter
can you just try it? No one is going to help you if any time someone tries to help you say “im not that good of a scripter.”
If you’re going to do this multiple times with different chances on each, I would recommend using 1-100 for less work.
If the ratio is equal, any numbers will work. For example, if you are trying to use a 50% chance, you could technically use 1-2 (if it is equal to 1), or 1-16 (if it is <= 8), etc… and it will still work the same way.
Here is an example code:
local part = script.Parent
part.Touched:Connect(function(touch)
if game.Players:GetPlayerFromCharacter(touch.Parent) then -- if the thing that touched the part is a player
local chance = math.random(1,100) -- or 1,5 if you're doing 60%
if chance <= 60 then -- if it falls between 1 and 60, which is 60% of 100
--what happpens
end
end
end)
so if i want multiple chances i could do
local coinchance = math.random(1,100)
local gemchance = math.random(1,100)
if coinchance <= 60 then
-- do something
if gemchance <= 30 then
-- do something
Yes, you can do that. That would mean the coin chance is 60% while the gem chance is 30% (I’m sure you got that down)
local randomNumber = math.random(1, 100)
if randomNumber <= 60 then
--give coin
elseif randomNumber > 60 and randomNumber <= 90 then
--give gem
elseif randomNumber > 90 then
--give diamond
end
Typically most random systems generate a single random number and use that to decide what reward should be given out.
for goodness sakes, try my method out!!! It allows you to do it with a table. Look at my post above, it’s just for this type of use.
If I am understanding correctly, you are trying to only get ONE result from the percentage. Am I correct? If so, I would need to revise my solution a tad.
It is evident he does not feel comfortable using your module. No need to continue to force it upon him.
im not forcing him to do anything. Im simply just suggesting a solution. It does the exact same thing he wants to happen.
With the photo you provided, it gives the working code for that already. Using the GetItem
function will return ONE of the items in the ItemList
table corresponding to the percentages given. Would you like further explanation on how that script works?
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.
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