When you touch a part there is a % chance of something happening. For example a 60% chance of getting 20 coins
Place this script inside of a part
local percent = 60 --change to whatever percent
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local chance = math.random(1,100)
if chance <= percent then
--Do whatever you need to do, the coins or whatever.
end
end
end)
could i do something like
local chance = math.random(3,5)
if chance == 3 then
proceed to give coins
since 3 divded by 5 is 0.60 which is 60
That could work too, I just provided code that will allow you to change the percent to any number between 1 and 100.
That would not work, because the chance of it being equal to 3 is the same as it being equal to 4 (or 2)
You need to use >= instead of ==
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?