Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Calling Function with Table?

Basically what I'm trying to do is take a value from a table based on which global variables are set to true and use it to call a function. Here's what I've got so far...

CheckMode = {"Normal", "Classic"}
ModeFunctions = {Normal, Classic}
--colored not needed table

function execute(func)
	func()
end
function Normal()
	--there's stuff in here, but it's not relevant to this post.
	--I'm just showing you this exists
end
function Classic()
	--same as above
end

checked = false
foundmode = false
while true do
	if checked == false then --Have we already done this?
		for i,v in pairs(CheckMode) do
			if _G[CheckMode[i]] == true then
				currentmode = i
				checked = true
				foundmode = true
			elseif foundmode == false then
				warn ("Mode Function not found! Assuming to be Colored...")
				currentmode = 1
				checked = true
			elseif foundmode == true then
				print "Already found mode!"
			end
		end
	end
	execute(ModeFunctions[currentmode]) --Attempt to call local 'func' (a nil value)
	--execute(Normal)
	wait (.5)
end

The line that errors is execute(ModeFunctions[currentmode]). But strangely enough, brute forcing the function works fine: execute(Normal)

Anybody have the solution for this? Thank you!

2 comments

0

Try moving line 2 to line 15, I think what's happening is the function does not exist when the table is created and therefore the values in the table are nil values.

0

This was the solution, thank you!

2 answers

✓ Accepted answer

Try moving line 2 to line 15, I think what's happening is the function does not exist when the table is created and therefore the values in the table are nil values.

It was exactly as UndeniableLimited said! The table needed to moved.

This discussion is preserved as it was. New answers, comments, and votes are closed.