I’ve been making an RNG game, and I was wondering how I could make a Luck Boost system.
Please explain thoroughly, and provide script examples.
My Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StatsFolder = ReplicatedStorage:WaitForChild("Stats")
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local activateRoll = EventsFolder:WaitForChild("ActivateRoll")
local messages = EventsFolder:WaitForChild("Messages")
local totalStats = StatsFolder:GetChildren()
local random = Random.new()
local function RNGCheck(PlayerLuck, Rarity)
local Roll = random:NextInteger(1, math.round(Rarity/PlayerLuck)) -- Picks a random number
if Roll <= 1 then
return true -- Player wins!
else
return false -- Player loses!
end
end
local function getRandomRarity(player)
player.leaderstats.Rolls.Value += 1
for i = 1, #totalStats do
if RNGCheck(player.leaderstats["Luck Value"].Value, totalStats[i].Value) == true then
print(totalStats[i].Name, true, player.Name)
if StatsFolder[totalStats[i].Name].Value >= 300 then
messages:FireAllClients(player.Name .. " has gotten " .. totalStats[i].Name .. "! (1/" .. StatsFolder[totalStats[i].Name].Value .. ")", StatsFolder[totalStats[i].Name].Color.Value)
end-- no it still should return
return StatsFolder[totalStats[i].Name]
end
end
return StatsFolder["Common"]
end
activateRoll.OnServerInvoke = function(player)
local t = {}
for i=1, workspace.GameInfo.RollAmount.Value - 1 do
table.insert(t, totalStats[math.random(#totalStats)])
end
table.insert(t, getRandomRarity(player))
return t
end
local function RNGCheck(PlayerLuck, Rarity, LuckBoostActive: boolean): boolean
local LuckBoost = LuckBoostActive and 2 or 1
local Roll = random:NextInteger(1, math.round(Rarity / (PlayerLuck * LuckBoost)))
end
local Egg = {
Dog = 0.3,
Cat = 0.3,
Parrot = 0.175,
Duck = 0.175,
Goose = 0.05,
}
-- Convert Egg to an array to be sorted
local pets = {}
for i, v in pairs(Egg) do
table.insert(pets,{i,v})
end
table.sort(pets, function(a, b)
return a[2] < b[2]
end)
-- Sort Egg into segments
local segments = {}
local total = 0
local luckboost = 1 -- increase value to increase luck
for _, data in pairs(pets) do
table.insert(segments, {total, total + data[2], data[1]})
total = total + data[2]
end
local depth = 10000 -- more depth allows for more preciseness for chance, but takes longer to compute
-- 1000 depth allows for chances like: 0.0001.
local function HatchEgg()
local x = math.random(0,depth) / depth
x = x / luckboost -- forces x to be closer to smaller segments
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Unknown"
end
for i = 1, 10 do
print(HatchEgg())
end
What did you set the luck boost? Also, it may be because you set the rarities incorrectly in the table. The more you increase the luck boost, the less and less you will see the common rarities to the point in which it’s impossible to get them.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StatsFolder = ReplicatedStorage:WaitForChild("Stats")
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local activateRoll = EventsFolder:WaitForChild("ActivateRoll")
local messages = EventsFolder:WaitForChild("Messages")
local totalStats = StatsFolder:GetChildren()
local depth = 100000
local rarities = {}
for i, v in pairs(totalStats) do
table.insert(rarities,{v.Name,v.Value})
end
table.sort(rarities, function(a, b)
return a[2] < b[2]
end)
local function SelectRarity(player)
player.leaderstats.Rolls.Value += 1
local segments = {}
local total = 0
local luckboost = player.Luck.Value or 1 -- increase value to increase luck
for _, data in pairs(rarities) do
table.insert(segments, {total, total + data[2], data[1]})
total = total + data[2]
end
print(segments)
local x = math.random(0,depth) / depth
x = x / luckboost -- forces x to be closer to smaller segments
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Unknown"
end
activateRoll.OnServerInvoke = function(player)
local t = {}
for i=1, workspace.GameInfo.RollAmount.Value - 1 do
table.insert(t, totalStats[math.random(#totalStats)])
end
table.insert(t, StatsFolder[SelectRarity(player)])
print(t)
return t
end
The way I did it, it is more rarer the higher the luck is.
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StatsFolder = ReplicatedStorage:WaitForChild("Stats")
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local activateRoll = EventsFolder:WaitForChild("ActivateRoll")
local messages = EventsFolder:WaitForChild("Messages")
local totalStats = StatsFolder:GetChildren()
local depth = 100000
local function SelectRarity(player)
local luckboost = player.Boosts.Luck.Value
local rarities = {}
for i, v in pairs(totalStats) do
table.insert(rarities,{v.Name,v.Value})
end
table.sort(rarities, function(a, b)
return a[2] < b[2]
end)
local segments = {}
local total = 0
for _, data in pairs(rarities) do
table.insert(segments, {total, total + data[2], data[1]})
total = total + data[2]
end
local x = math.random(0,depth) / depth
x = x / luckboost
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Common"
end
activateRoll.OnServerInvoke = function(player)
player.leaderstats.Rolls.Value += 1
local t = {}
for i=1, workspace.GameInfo.RollAmount.Value - 1 do
table.insert(t, StatsFolder[SelectRarity(player)])
end
local s = StatsFolder[SelectRarity(player)]
if s.Value >= 750 then
messages:FireAllClients(player.Name .. " has gotten " .. s.Name .. "! (1/" .. s.Value .. ")")
end
table.insert(t, s)
print(s)
return t
end
There we have it, a trait of game developers. Not making a new thread and asking a person who is also asking for help. Honestly, I would make a thread, you could get better coders to help answer.
ive been flagged for “remaking a similar post” that i dont want to again for fear ill actually get banned (im on my like 6th warning) but yeah, if you u could help explain that first bit that would be be a big help.