I’m making a tag game, and I just added modifiers. I’ve been wondering how to make a function that gives it a chance to get 1-10 modifiers for a round. How may I go about doing that?
can you elaborate more on that?
Like, I want a function that automatically sets the chances for each modifier from 1-10 modifiers. For example,
local chances = {}
as the start, then
local chances = {
[1] = 2,
[2] = 2, --a number
--etc.
}
Sorry if I respond late, I’m also working on multiple things.
Do you wish for a function that creates 1-10 random modifiers for a round or something else? It seems that no one can really understand what you want.
To me, it’s really hard to explain. What I can’t figure out how to explain, is a formula/function that creates a dictionary of chances to determine how many modifiers will be in the round.
So you want an array of chances to determine how many modifiers there will be in a round? Well, from my understanding you don’t really need an array for that, you can use math.random(1, 10)
to get a random number between 1 and 10 that will determine how many modifiers will be in a round.
But, I want to make it that the more modifiers they want in the round to be more rarer. Like if the chance is different based on the number.
-- Haven't tested might not work first try but should be easy to edit
local chances = {
[1] = 2;
[2] = 2;
} -- Fill this out
local random = Random.new()
local function getModifiers(minNumber:number, maxNumber:number)
local amount = random:NextInteger(minNumber, maxNumber)
local selectedModifiers = {}
repeat
local selectedIndex = random:NextInteger(1, #chances)
local modifier = chances[selectedIndex]
if not table.find(selectedModifiers, modifier) then
table.insert(selectedModifiers, modifier)
amount -= 1
else
task.wait()
end
until amount <= 0 or #selectedModifiers >= #chances
return selectedModifiers
end
So, to explain the script; the function creates a table to store the created modifiers. It then decides how many modifiers it wants to create (using the min and max numbers). It then loops and picks a random index from the chances table you provided and if it’s not already added, it adds it and removes 1 from the “amount”. I added the task.wait() so it doesn’t crash your roblox or something if it can’t find an unusable modifier.
Hopefully it won’t crash your Roblox and hopefully it works. This could definitely be made faster but I’m not spending too much time on this.
I don’t quite get what you mean, can you give us an example of a scenario, please?
I’ve already had a system for that. What I’m trying to say, is I want a functipn that creates the chance INSIDE the chances table, so that I can just change 2 variables,
local chanceStep = 0.2 -- or something, chance between each modifier. 2 -> 1.8
local startChance = 10, -- makes the first modifier being 1/10
and it would automatically update. (If you want, you can add more variables to make this more accurate)
Pretty close. I guess I could use it? I’ll have to test it though. (I will do it tomorrow, I have to sleep.)
. This function will automatically populate the chances
table based on these variables.
local function generateChances(startChance, chanceStep, totalModifiers)
local chances = {}
for i = 1, totalModifiers do
chances[i] = startChance - ((i - 1) * chanceStep)
if chances[i] < 0 then
chances[i] = 0 -- Ensuring no negative chances
end
end
return chances
end
-- Configuration
local startChance = 10 -- Starting chance for the first modifier
local chanceStep = 0.2 -- Decrease in chance for each subsequent modifier
local totalModifiers = 10 -- Total number of modifiers
-- Generating the chances table
local chances = generateChances(startChance, chanceStep, totalModifiers)
-- To print and verify the chances table
for key, value in pairs(chances) do
print("Modifier " .. key .. " has a chance of " .. string.format("%.1f", value))
end
In this script, generateChances
is a function that takes three parameters: startChance
, the chance for the first modifier; chanceStep
, the decrement in chance for each subsequent modifier; and totalModifiers
, the total number of modifiers you want to generate chances for.
This setup allows you to easily modify the starting chance and the step between each modifier without manually adjusting each entry. The function also includes a simple check to ensure that no chance values become negative, by setting any negative values to 0. You can adjust this behavior based on your needs (for instance, allowing certain modifiers to have no chance or to set a minimum chance value other than 0).
to add more variables or modify the function to better suit your specific requirements, such as incorporating a minimum chance limit or different decrement strategies for the chance values.
Thank you so much! It works. This function you’ve made is very customizable Thanks! Just have to figure out how I can make a function to get the modifier count.
local function GetModCount()
for key, value in pairs(chances) do
if math.random(0, value) == 0 then
return key
end
end
return 1
end
Would this be good?
That can be a good way to customize but whatever fits you
Honestly, I just want the most efficient way to get the modifier count. But it’s good enough for now.
Yeas glad that helps but yes generation lo
DEFINITELY DUDE
ALSO ADD ME WE CAN Collaborate