Function name containing a variable how to call

hi,

how do i call a functionname containing a value like this bellow, and i dont want to use if statments

function fill_0(part)
end

function fill_1(part)
end

function fill_2(part)
end

function fill_3(part)
end

–how ? the value will change and more numbers will come so i need this to work
value = 2

fill_[value](partinworkspace)

^ tried other combinations doesn’t seem to work anyone know the working format

As Roblox compiles your program before it’s executed, this method is actually not possible.

I suggestion creating a single function fill with one of the parameters being the number. Then you can create additional snippets of code within the function that run depending on this parameter. For example:

function fill(Type, Part)
     if Type == 1 then
          -- snippet 1
     elseif Type == 2 then
          -- snippet 2
     end
end

fill(Type, partinworkspace)

Another solution would be to create an array with different functions inside and simply index them using the type:

local Functions = {
     function(Part)     
          -- code snippet     
     end,

     function(Part) 
         -- code snippet
     end,
}

Function[Type](partinworkspace)

Hope this helps,
-Tom :slight_smile:

ok ty din’t find any example to the idea i wanted to work ill do it different then ty

If you’re happy, the little checkbox underneath my post will mark it as a solution. This helps me, and helps others know this thread is complete.

1 Like

basically what Thomas said:

you actually can do this by using a table:

local functionTable = {}

function functionTable.fill_0(part)
end

function functionTable.fill_1(part)
end

function functionTable.fill_2(part)
end

function functionTable.fill_3(part)
end


value = 2

functionTable["fill_" .. value](partinworkspace) -- works
1 Like
--[[
0 meteor
1 grass planet
2 sand planet
3 frost planet
]]--



materialnumber = 1
function fill_0(part)
	local material = {Enum.Material.Slate,Enum.Material.Rock,Enum.Material.Air}
	local bufferA = math.random(1,100)
	if bufferA <97 then
		materialnumber = math.random(1,2)
	else
		materialnumber = 3
	end
	game.Workspace.Terrain:FillBlock(part.CFrame,Vector3.new(part.Size.X*math.random(1,1.4),part.Size.Y*math.random(0.5,2),part.Size.Z*math.random(1,1.2)), material[materialnumber]) part:remove()
end
function fill_1(part)
	game.Workspace.Terrain:FillBlock(part.CFrame,part.Size, Enum.Material.Grass) part:remove()
end
function fill_2(part)
	game.Workspace.Terrain:FillBlock(part.CFrame,part.Size, Enum.Material.Sand) part:remove()
end
function fill_3(part)
	game.Workspace.Terrain:FillBlock(part.CFrame,part.Size, Enum.Material.Snow) part:remove()
end
function fillplanet(Type,part)
	if Type == 0 then fill_0(part) end
	if Type == 1 then fill_1(part) end
	if Type == 2 then fill_2(part) end
	if Type == 3 then fill_3(part) end
end
function createPlanet(Core,PlanetType)
	local size = ((Core.Size.X+Core.Size.Y+Core.Size.Z)/10)
	a = 0
	for i = 1,31 do
		a = a-0.1
		if i == 16 then
			a = 0
		end
		if i <= 15 then
			a = a + 0.2
		end
		if size > 75 then
			wait()	
		end
		for angle = 1, 360, 10 do
			local p = Instance.new('Part')
			p.Parent = Core.Parent
			p.Size = Vector3.new(size,size,size+50)
			p.Anchored = true
			p.Transparency = 0.5
			p.CFrame = Core.CFrame
			* CFrame.Angles(math.rad(angle), a, 0)
			* CFrame.new(0, 0,size*5)
			fillplanet(PlanetType,p)
			
		end
	end
	game.Workspace.Terrain:FillBall(Core.Position,(((Core.Size.X+Core.Size.Y+Core.Size.Z)/3)/2)*3.14, Enum.Material.Basalt)
	game.Workspace.Terrain:FillBall(Core.Position,(((Core.Size.X+Core.Size.Y+Core.Size.Z)/3)/2)*1.8, Enum.Material.CrackedLava)
	Core:remove()
end




galaxy = game.Workspace.planets
planets = galaxy:GetChildren()
for i = 1,#planets do
	wait()
	if planets[i]:FindFirstChild("Data") ~= nil then
		local planetmodel = Instance.new("Model")
		planetmodel.Name = planets[i].Data.Value
		planetmodel.Parent = galaxy
		planets[i].Parent = planetmodel
		createPlanet(planets[i],planets[i].Type.Value)
	end
end

ill try it tomorrow i am using it for making random planets with different variations of biomes ty all for the help