How do I do this?

I wanna make a tool that gives you random “mutations” but I’m a bit stumped on how to do it without spaghetti code

local spawning = {}

function spawning.RandomizeMutation(chances)
	local randomizeditems = {
		4;
		6;
		10;
		15;
	25;
	40;
	}
	chances = randomizeditems
	return chances
end
function spawning.MutationList(List)
	local spawnlist = {
		"Swiftness",
		"Slowness",
		"Damage",
		"Firerate Debuff",
		"Firerate Buff",
		"Gigantism",
		"Enlarged arm",
	}
	List = spawnlist
	return List
end
return spawning

This is what I’ve tried to do to combat writing spaghetti code, but I can’t really figure out how to continue from here. Could somebody help?

2 Likes

Are you trying to pick a random item from the list? If so, you would do:
local randomMutation = spawnList[math.random(1, #spawnList)]

2 Likes

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?

1 Like

Are you asking how to apply the mutations?
ex. Swiftness

1 Like

Yeah I’m like asking how to apply them without building spaghetti hellhole code

1 Like

By spaghetti code do you mean like a bunch of if statements?

like

if mutation == "Swiftness"

elseif mutation == "slowness"

ect.
1 Like

Yeah that’s what I mean, is there any way to get out of using that or is it inevitable?

1 Like

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”

1 Like

You can use module scripts to organize your code

Set up your script something like this:
image

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")

Your modules should look like this:

return function(player:Player)
	-- do whatever
end
1 Like

Also; why do your fuctions look like that?

you can just set all the data like this:

spawning.Mutations = {
		["Swiftness"] = 4,
		["Slowness"] = 6,
		["Damage"] = 10,
		["Firerate Debuff"] = 15,
		["Firerate Buff"] = 25,
		["Gigantism"] = 40,
		["Enlarged arm"] = 40,
}

And use a Weighted Chance System to add rarity

3 Likes

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
2 Likes

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
1 Like