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

[Solved] Unable to require a module script from a table?

Trying to make a task system, it's supposed to get a random task from a table and require it, but it doesn't. Task keeps returning as "None"

local tasks = {
	"DigitsOfPi",
	"Jump",
	"ZeroHealth"
}
script.Parent.Triggered:Connect(function(player)
	print("triggered")
	if player.Task.Value == "None" then -- a string value
		local taskLabel = player:WaitForChild("Task")
		local task = tasks[math.random(1,#tasks)] -- gets name of task
		local assignedTask = require(script[task]) -- requires task
		print("random") -- printing stops here, so i am guessing that it does not require
		if assignedTask.Name == "DigitsOfPi" then
			print("digits of pi")
			taskLabel.Value = "Chat 200 digits of pi"
			assignedTask.Task(player,100)
		elseif assignedTask.Name == "Jump" then
			print("jump")
			taskLabel.Value = "Jump 100 times"
			assignedTask.Task(player,500)
		elseif assignedTask.Name == "ZeroHealth" then
			print("die lol") -- dont mind this
			taskLabel.Value = "Reach 0 health"
			assignedTask.Task(player,1000)
		end
	end
end)

I've tried using script.DigitsOfPi etc etc, but it still won't work. Is there something I am doing wrong? Thanks in advance

4 comments

1

Unless you have a 'Name' attribute in the Table you're returning from the ModuleScript, then your conditionals referring to it are going to fail; require inherits the Table, not the Script.

0

I am confused

0

Im not requiring the table, Im requiring the script...it gets the random name, and I am no longer using the table after that?

0

OH WAIT I FIGURED IT OUT. THANK YOU. I WAS HAVING TROUBLE WITH WHAT YOU SAID AND THEN IT CAME TO ME. Youre right, It is accessing the table in the module script, not the actual modulescript......THANK U

1 answer

assignedTask is within the event. Thus, everytime the event fires, the variable will not exist. A solution is just simply put the variable outside of the event.

local tasks = {
    "DigitsOfPi",
    "Jump",
    "ZeroHealth"
}
local assignedTask
script.Parent.Triggered:Connect(function(player)
    print("triggered")
    if player.Task.Value == "None" then -- a string value
        local taskLabel = player:WaitForChild("Task")
        local task = tasks[math.random(1,#tasks)] -- gets name of task
        assignedTask = require(script[task]) -- requires task
        print("random") -- printing stops here, so i am guessing that it does not require
        if assignedTask.Name == "DigitsOfPi" then
            print("digits of pi")
            taskLabel.Value = "Chat 200 digits of pi"
            assignedTask.Task(player,100)
        elseif assignedTask.Name == "Jump" then
            print("jump")
            taskLabel.Value = "Jump 100 times"
            assignedTask.Task(player,500)
        elseif assignedTask.Name == "ZeroHealth" then
            print("die lol") -- dont mind this
            taskLabel.Value = "Reach 0 health"
            assignedTask.Task(player,1000)
        end
    end
end)

1 comment

0

Thanks for the answer, but I already have the variable when i identify as "local"

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