Yeah but how would I go from here? Like I now have the mutation but how would I make the mutations work without writing spaghetti code?
Is it inevitable and am I just forced to write spaghetti code?
Unfortunately, it is inevitable. The computer is not AI and if you simply go to a computer and tell it “Swiftness” it would not know what to do with that information. You have to specify what you mean by “Swiftness”
now in your main script; you can apply mutations like this:
local mutations = script.Mutations
local function applyMutation(player:Player, mutation:string)
local fn = mutations:FindFirstChild(mutation) and require(mutations[mutation])
if not fn then
warn("Invalid mutation", mutation)
return
end
fn(player)
end
applyMutation(player, "Gigantism")
Create a function for each mutation and run them in accordance to the random index selected from the mutations table.
local function Swiftness()
end
local Mutations = {
["Swiftness"] = -- Swiftness
}
local SelectedMutation = Mutations[1]() -- Runs the swiftness function
what I would do is give every Mutation a function value and a keys table that will store the names of the keys with number indexes of the Mutation table and use that to get a random string and use it to execute a random function so that I can avoid writing many if statements
local Mutations = {}
Mutations.List = {
["Damage"] = function()
-- damage code
end,
["Speed"] = function()
-- speed code
end,
["Slowness"] = function()
-- slowness code
end,
-- etc
}
local mutationKeys = {}
for key in Mutations.List do
table.insert(mutationKeys, key)
end
-- mutationKeys will be like this now
--[[
{
[1] = "Damage",
[2] = "Speed",
[3] = "Slowness"
}]]
function Mutations.ActivateRandoMutation()
local randomKey = mutationKeys[math.random(1, #mutationKeys)]
Mutations.List[randomKey]()
end
return Mutations