So my game has a system where you spawn with a “race” that gets abilities. You get a random race when you spawn and some are more rare than others. You can then purchase race rerolls in the game to change your race however I have 2 issues.
Problem 1:
First of all, this is the race chances table
local races = { -- ["Race"] = Chance (%)
["Corvus"] = 2,
["Jinn"] = 2,
["Aquaran"] = 4,
["Hollow"] = 4,
["Aurorian"] = 5,
["Phantom"] = 6,
["Cactacae"] = 7,
["Dulask"] = 10,
["Colubran"] = 11,
["Golem"] = 14,
["Magus"] = 17,
["Mimiga"] = 18,
}
return races
It’s obviously a module script that gets the race chances.
So when you reroll one problem occurs. What if you already own that race? Well to solve this issue I simply get your current race and get its % then split it and add it to the other race chances and remove your current race from the pool.
This is the code to roll your race and it also splits the race chances like I mentioned as you can see:
local RS = game:GetService("ReplicatedStorage")
local RaceChances = require(RS.Modules:WaitForChild("RaceChances"))
local function deepCopy(original)
local copy = {}
for k, v in pairs(original) do
if type(v) == "table" then
v = deepCopy(v)
end
copy[k] = v
end
return copy
end
local function rollRace(plr)
local chances = deepCopy(RaceChances)
if plr and chances[plr.data.Race.Value] then
local currentRaceChance = chances[plr.data.Race.Value]
chances[plr.data.Race.Value] = nil
local totalRaces = 0
for _,_ in pairs(chances) do
totalRaces += 1
end
for index,_ in pairs(chances) do
chances[index] += currentRaceChance/totalRaces
end
print(chances)
print("UPDATED RACE CHANCES")
end
local total = 0
local random = math.random(1,100)
local result = nil
for race,chance in pairs(chances) do
total += chance
if random <= total and result == nil then
result = race
end
end
if total ~= 100 then
warn("RACE CHANCES DONT ADD UP TO 100, ADDS UP TO: "..tostring(total))
print(chances)
end
return result
end
return rollRace
As per ROBLOXs guidelines I am required to show race percentages. There is no issue with this except when the race chances get split with your current race it creates big decimals. Example:
I obviously cant just round them otherwise I’d technically be breaking roblox guidelines since they aren’t accurate. I also don’t want to do that personally even if it were legal. How can I make it so the % split doesnt create large decimals? Aiming to have single decimals or double digit decimals at the most.
Thats problem 1
Problem 2:
This also has to do with the current race being split
When it splits, and only SOMETIMES with certain races I get this warning which I made:
All of the race chances wont add up to exactly 100 and sometimes will be subtracted VERY slightly or added onto VERY slightly. Why is this and how can I fix it?
All help is greatly appreciated thank you in advance for any help given!