Basically I’m making it so when the player touches the part, it makes it so it gives them a badge based on the chance of that badge, I wanna make it so when the player gets something, it shows them what they got by the text. for example..
YOU GOT:
IMAGE
LUCK SOCK! (1% CHANCE)
This is how I want it to be. I already have the top text and bottom text and the image set up, I just need help with this probability. I also need it to be made so if you already have the badge, then the probability for the ones you don’t have yet are higher, (rare ones get a bit higher).
Script:
local part = script.Parent
local gui = part:FindFirstChild("reward")
local frame = gui:FindFirstChild("rewardframe")
local a = frame:FindFirstChild("a") -- text (DONT EDIT)
local b = frame:FindFirstChild("b") -- image
local c = frame:FindFirstChild("c") -- text (EDIT)
local sound = part:FindFirstChild("victory.wav")
local touched = false
local skins = {
["Nothing"] = {
Chance = 70,
ImageId = 0,
BadgeId = "none"
},
["Bingo"] = {
Chance = 15,
ImageId = 114065736352419,
BadgeId = 2810946539220846
},
["Luck Sock"] = {
Chance = 1,
ImageId = 82668483607335,
BadgeId = 804836648963859
},
["RNG Gift"] = {
Chance = 10,
ImageId = 82455403952408,
BadgeId = 3718379134595863
},
["Percy"] = {
Chance = 10,
ImageId = 74703244469978,
BadgeId = 1716393701325699
},
["The Mandrill"] = {
Chance = 9,
ImageId = 12457659547,
BadgeId = 161456184884951
}
}
local function giveReward(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
sound:Play()
-- idk what to do here
end
end
To calculate the probability to receive each item you need to divide the chance of that item with the total of the chances of all of the items. This will give you a normalized decimal between 0 and 1. To increase the player luck you need to increase the chances of the rare items while lowering the chances of the common ones (this can be done in multiple ways depending on intended behavior). If you want to lower the chance of specific item you either decrease that item chance or increase the chance of everything else (or both). You can also setup a system with weighted chances that increases the luck to get a rare item depending on how many times a user has attempted to get it.
In general when creating luck based systems there are many areas you can get creative over and add logical biases on top of (so players don’t lose their interest).
If you want to easily be able to know the percentage you need to have the total of all the possibilities to add to 100. This means that if the possibility is 15 then that equates to 15%(assuming your logic is done correctly).
If all your possibilities need to add up to more/less than 100 then you need divide the possibility by the total possibility’s, multiplied by 100. An example is if all your total possibilities add to 200, to calculate the percent of 15 is to 200, you do this (15/200) * 100 = 0.075 * 100 = 7.5%.
Use Random.NextIntegral(1, 115) and get random number between 1 and 115(sum of all Chances).
· If it is between 1 and 70, “Nothing”
· If 71 to 85, “Bingo”
· If 86 , “Luck Sock”
· If 87 to 96, “RNG Gift”
· If 97 to 106, “Percy”
· If 107 to 115, “The Mandrill”
Hardcoding the totals of the chance is a really bad way to go about this. You should add up all the chances like this:
local function selectRandom()
local TotalChance = 0
for _, entry in pairs(skins) do
TotalChance = TotalChance + Point.Chance
end
local randomNum = math.random(1, TotalChance)
local cumulative = 0
local selectedTier = nil
for _, entry in pairs(skins) do
for tierName, tierData in pairs(entry) do
cumulative = cumulative + tierData.Chance
if randomNum <= cumulative then -- if the random number is lower than or equal to cumulative(The loop wouldve broken before if they random number is lower than cumulative.)
selectedTier = tierName
break
end
end
if selectedTier then break end -- stop if found
end
end
In this example ‘selectedTier’ should print the name/index of the randomly selected entry from the skins table. Also I have not tested this code so it may not fully work like that.
Theres an issue. I made it working but it always gives me the rare ones, not even nothing which is a higher chance. I would also like if you already have the badge of a entry, it increases the chances for the other ones so it equals 100 in all chances together (luck sock doesn’t get increased)
Script:
local part = script.Parent
local gui = part:FindFirstChild("reward")
local frame = gui:FindFirstChild("rewardframe")
local a = frame:FindFirstChild("a") -- text (DONT EDIT)
local b = frame:FindFirstChild("b") -- image
local c = frame:FindFirstChild("c") -- text (EDIT)
local sound = part:FindFirstChild("victory.wav")
local touched = false
local skins = {
["Nothing"] = {
Name = "Nothing",
Chance = 70,
ImageId = 0,
BadgeId = "none"
},
["Bingo"] = {
Name = "Bingo",
Chance = 5,
ImageId = 114065736352419,
BadgeId = 2810946539220846
},
["Luck Sock"] = {
Name = "Luck Sock",
Chance = 1,
ImageId = 82668483607335,
BadgeId = 804836648963859
},
["RNG Gift"] = {
Name = "RNG Gift",
Chance = 5,
ImageId = 82455403952408,
BadgeId = 3718379134595863
},
["Percy"] = {
Name = "Percy",
Chance = 10,
ImageId = 74703244469978,
BadgeId = 1716393701325699
},
["The Mandrill"] = {
Name = "The Mandrill",
Chance = 9,
ImageId = 12457659547,
BadgeId = 161456184884951
}
}
local function selectRandom(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
local TotalChance = 0
for _, entry in pairs(skins) do
TotalChance = TotalChance + entry.Chance
end
local randomNum = math.random(1, TotalChance)
local cumulative = 0
local selectedTier = nil
for _, entry in pairs(skins) do
for tierName, tierData in pairs(entry) do
cumulative = cumulative + entry.Chance
if randomNum <= cumulative then -- if the random number is lower than or equal to cumulative(The loop wouldve broken before if they random number is lower than cumulative.)
selectedTier = tierName
b.Image = "rbxassetid://"..entry.ImageId
c.Text = entry.Name.."! ("..entry.Chance.."% Chance)"
if entry.BadgeId ~= "none" then
game:GetService("BadgeService"):AwardBadgeAsync(player.UserId, entry.BadgeId)
end
break
end
end
if selectedTier then
break
end
end
end
end
local function giveReward(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= nil then
if touched == true then return end
touched = true
selectRandom(hit)
part.CollisionGroup = "plrs"
part.Transparency = 1
if part:FindFirstChild("Highlight") then
part:FindFirstChild("Highlight"):Destroy()
end
for _, p in pairs(part:GetDescendants()) do
if p:IsA("ParticleEmitter") then
p:Destroy()
end
end
task.delay(5, function()
part:Destroy()
end)
gui.Enabled = true
sound:Play()
end
end
part.Touched:Connect(function(hit)
giveReward(hit)
end)
could you not use math.random() and reserve a set of numbers for each probabiltiy?
for example
local probabiltiy = math.random(1,103) -- 100 + number of items
if probabiltiy >= 1 and probabiltiy <= 70 then
print("Nothing")
elseif probabiltiy >= 71 and probabiltiy <= 81 then --add 1 per item
print("Percy")
elseif probabiltiy >= 82 and probabiltiy <= 91 then
print("The Mandrill")
--etc.
end
when you’d do this you’d obviously add all the variable and make sure the highest number equals the max number in the math.random().
in this way the chances to get each item are still the same.
(if this makes no sense pls lmk.)