I dont know how to solve the attempted to call require with invalid argument(s) error

I dont know how to solve this error:

This is the TriviaGame Script

-- Requiere el script "QuestionDatabase"
local QuestionDatabase = require(script.Parent.QuestionDatabase)

-- Objeto "TriviaGame"
local TriviaGame = {
	CurrentQuestion = nil,
	Score = 0,
	GameInProgress = false,
}

-- Variable para almacenar el tiempo de espera antes de iniciar un nuevo juego
local WAIT_TIME = 10 -- Cambia este valor si deseas ajustar el tiempo de espera

-- Función para seleccionar una pregunta aleatoria del QuestionDatabase
function TriviaGame:SelectRandomQuestion()
	local totalQuestions = #QuestionDatabase.Questions
	local randomIndex = math.random(1, totalQuestions)
	self.CurrentQuestion = QuestionDatabase.Questions[randomIndex]
end

-- Función para mostrar la pregunta a un jugador específico
function TriviaGame:DisplayQuestion(player)
	-- Obtener la pantalla del jugador donde se mostrará la pregunta
	local playerScreen = player:WaitForChild("Screen") -- Asegúrate de tener un objeto llamado "Screen" dentro del jugador

	-- Crear un texto para mostrar la pregunta
	local questionText = Instance.new("TextLabel")
	questionText.Text = self.CurrentQuestion.QuestionText
	questionText.Size = UDim2.new(1, 0, 0.5, 0)
	questionText.Position = UDim2.new(0, 0, 0, 0)
	questionText.Parent = playerScreen

	-- Crear un texto para mostrar las posibles respuestas
	local answersText = Instance.new("TextLabel")
	answersText.Text = table.concat(self.CurrentQuestion.Answers, "\n")
	answersText.Size = UDim2.new(1, 0, 0.5, 0)
	answersText.Position = UDim2.new(0, 0, 0.5, 0)
	answersText.Parent = playerScreen

	-- Desactivar el salto del jugador mientras el juego está en progreso
	player.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end

-- Función para comprobar la respuesta del jugador
function TriviaGame:CheckAnswer(player, selectedAnswerIndex)
	if selectedAnswerIndex == self.CurrentQuestion.CorrectAnswerIndex then
		-- Respuesta correcta, incrementar la puntuación del jugador
		self.Score = self.Score + 1
		-- Puedes agregar aquí alguna lógica adicional, como mostrar un mensaje de felicitaciones al jugador
	else
		-- Respuesta incorrecta
		-- Puedes agregar aquí alguna lógica adicional, como mostrar un mensaje de error al jugador
	end

	-- Borrar la pantalla después de que se haya respondido la pregunta
	local playerScreen = player:WaitForChild("Screen")
	playerScreen:ClearAllChildren()

	-- Seleccionar una nueva pregunta después de que se haya respondido la actual
	self:SelectRandomQuestion()

	-- Verificar si el juego ha terminado
	if self.Score >= 5 then -- Por ejemplo, el juego termina cuando el jugador alcanza una puntuación de 5
		self:EndGame()
	else
		-- Continuar mostrando preguntas mientras el juego no haya terminado
		self:DisplayQuestion(player)
	end
end

-- Función para terminar el juego
function TriviaGame:EndGame()
	-- Restaurar la capacidad de salto del jugador
	local players = game:GetService("Players"):GetPlayers()
	for _, player in ipairs(players) do
		player.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	end

	-- Esperar antes de iniciar un nuevo juego
	wait(WAIT_TIME)

	-- Iniciar un nuevo juego
	self:StartGame()
end

-- Función para sentar a los jugadores en la silla
function TriviaGame:SeatPlayers()
	local seat = workspace:FindFirstChild("Seat") -- Asegúrate de tener un objeto llamado "Seat" en el workspace

	if seat then
		local players = game:GetService("Players"):GetPlayers()
		for _, player in ipairs(players) do
			local character = player.Character
			if character then
				local humanoid = character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid.Jump = false
					humanoid.Sit = seat
				end
			end
		end
	end
end

-- Función para esperar a que los jugadores carguen el juego antes de comenzar
function TriviaGame:WaitForPlayers()
	local players = game:GetService("Players")
	players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(character)
			local humanoid = character:WaitForChild("Humanoid")
			humanoid.Jump = false
		end)
	end)
end

-- Función para iniciar el juego
function TriviaGame:StartGame()
	self.Score = 0
	self.GameInProgress = true
	self:SelectRandomQuestion()
	self:SeatPlayers()

	-- Mostrar preguntas a todos los jugadores
	local players = game:GetService("Players"):GetPlayers()
	for _, player in ipairs(players) do
		self:DisplayQuestion(player)
	end
end

-- Llamar a la función WaitForPlayers para esperar a que los jugadores carguen el juego
TriviaGame:WaitForPlayers()

-- Llamar a la función StartGame para iniciar el juego cuando se desee
TriviaGame:StartGame()

-- Contenido del script TriviaGame

-- Aquí iría todo el código y la lógica del juego TriviaGame

-- Al final del script, devuelve el objeto que deseas requerir
return TriviaGame





And the MainGame Script:
1 Like

And the Main Script

-- En el script "MainScript"

-- Requerir el script "QuestionDatabase" ubicado en el "ServerScriptService"
local QuestionDatabase = require(game:GetService("ServerScriptService").QuestionDatabase)

-- Requerir el script "TriviaGame" ubicado en el "ServerScriptService"
local TriviaGame = require(game:GetService("ServerScriptService").TriviaGame)

Where is the issue because you have provided more then one bit of code which has require() in it. If it happens in all of them could it be that the code your trying to require has yet loaded? If so that possible try to wait for it to load first before you require it?

I have a MainScript that find the scripts, the trivia game script and the questionDatabase script and I tried to when you join the game the main script locate the trivia game script and the question database and start the trivia game script and the question database script and give me that error

Is the question database a ModuleScript so that it can be required?

1 Like

its a normal script, it needed to be a module script only the Question Database?

1 Like

It does. You can transfer data in other ways, but a module script is probably best in this case

1 Like

I solved the error thanks

But, how can I solve the Main Script, its a script module too, can you help me I dont know how to solve the other error?

-- En el script "MainScript"

-- Requerir el script "QuestionDatabase" ubicado en el "ServerScriptService"
local QuestionDatabase = require(game:GetService("ServerScriptService").QuestionDatabase)

-- Requerir el script "TriviaGame" ubicado en el "ServerScriptService"
local TriviaGame = require(game:GetService("ServerScriptService").TriviaGame)


1 Like