How do I put functions inside a table?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A mechanism where a random quest(function) is chosen at random intervals

  2. What is the issue?
    Functions activate immediately and is not chosen randomly at random intervals

Sadly, I’ve been toying around with this for the entire day, and it’s clear that I’m illiterate in tables…

local RS = game:GetService("ReplicatedStorage")
local Quest = RS:FindFirstChild("Quest")
local Frame = game.StarterGui.ScreenGui.QFrame
local Quests = game.StarterGui.ScreenGui.QFrame.ScrollingFrame
local questlist = game.StarterGui.ScreenGui.QFrame.QuestList


Quest.OnServerEvent:Connect(function(player, gui)
while true do
	
	local function bandit()
		local banditquest = questlist:FindFirstChild("HuntBandit"):Clone()
  	  banditquest.Parent = gui
  	  banditquest.Visible = true
  	  print("Bandit!")
	end

	local function looter() 
	    local lootquest = questlist:FindFirstChild("HuntLooter"):Clone()
 	   lootquest.Parent = gui
 	   lootquest.Visible = true
  	  print("Looter!")
	end

local questlist = {{bandit()}, {looter()}}

local Random = math.random(5, 15)
local PickedQuest = math.random(1, #questlist)
print(questlist[PickedQuest])
wait(Random)

end
end)

1 Like
local functiontable = {}
functiontable["name of function"] = function(passedthrough,morepassedthrough)
print(passedthrough,morepassedthrough)
end
functiontable["name of function"]("Hello", "World!")
--or
functiontable.test = function(var)
print(var)
end
functiontable.test("test")

Also, you are setting your questlist variable twice.

5 Likes

Let’s first get onto the issues of how you are accesing UI’s. In order for your changes to be seen by the server and the client, you need to change properties of the players PlayerGui, effecting UI’s inside of the StarterGui will not replicated since when a player joins all GUI inside of StarterGui is cloned and parented to the players PlayerGui.

The example below will show you the base of where you should be calling for a players UI.

Example:

local PlayerGui = player:FindFirstChild("PlayerGui")

As mentions by @UltChowsk you are setting your questlist variable twice, so this should be erroring for you when you try and clone the object.


As for the problem with your functions, since you are creating a variable each time the loop runs, the functions get called automatically. You also are not waiting before you are running the function at the end of your script, so on server startup when the event is fired it will run instantly.

I would also for the future advise against using while true do loops inside of an event

This should be a solution, remember to change your variable.

----------------------------------------------------------------
--	CHANGE VARIABLES TO INSIDE THE EVENT AND USE PLAYERGUI 	  --	
----------------------------------------------------------------

local function bandit()		
	print("Bandit!")
end

local function looter() 
	print("Looter!")
end
	
local questlist = {bandit, looter} -- Only variables are added, removed () because that will run the functions

Quest.OnServerEvent:Connect(function(player, gui)
	while true do
		local Random = math.random(5, 15)
		local PickedQuest = math.random(1, #questlist)
		
		wait(Random)
		questlist[PickedQuest]()	
	end
end)
24 Likes