I’m trying to make a system that says random text when the player dies, I have 5 quotes and I have different percentages for them!
R.I.P 30/100
WASTED 20/100
GG’s 20/100
You died try again! 20/10
HOLEE 10/100
I want to know how to make the 5 text strings have a certain percent chance to print the text string (not a number) I am pretty bad at scripting…
Unfortunately making it percentage is super hard, because you have to manage every thing’s percent.
The approach i have in this is add something to a list a certain number of times (so it gets that chance of being picked)… This is how many big games do it (like Islands drop systems) (i think?).
local Quotes = {
{
Text = "something";
Chance = "10"
};
}
local IGQuotes = {}
local Randomizer = Random.new()
for i,v in pairs(Quotes) do
table.insert(IGQuotes, v.Text)
end
function ChooseQuote()
return IGQuotes[Randomizer:NextInteger(1, #IGQuotes)]
end
You could do percentages too but at the end of the day its random selection…
Hope it helps:
local quotes =
{
"Something",
"Something",
"Something"
}
function selectrandom()
local highest
for i,v in pairs(quotes) do
highest = i
end
local random = math.random(1, highest)
for i,v in pairs(quotes) do
if i == Random then
return v
end
end
end
local randomDeathMessage = selectrandom() -- This will referecnce the random quote selected as a string
This works for a pure random selection (though, you could just use quotes[math.random(#quotes)], however, OP wants weighted randomness.
Here’s the method I typically fall back on for weighted chance - it’s very easy to add new messages in the future, and to adjust weights if need-be.
local deathMessages = {
["R.I.P"] = 3;
["WASTED"] = 2;
["GG's"] = 2;
["You died try again!"] = 2;
["HOLEE"] = 1;
}
function GetRandomDeathMessage()
local pool = {}
for message,weight in pairs(deathMessages) do
for i = 1,weight do
table.insert(pool,message)
end
end
return pool[math.random(#pool)]
end
print(GetRandomDeathMessage())