You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? I wan
-
What is the issue? My Http script keeps erroring
-
What solutions have you tried so far? I’ve tried a lot of things and it didn’t work. This is my script. It uses the open trivia API
\
module.repf = game:GetService('ReplicatedFirst')
module.question = ""
module.WrongAnswers = {}
module.CorrectAnswer = ""
module.sers = game:GetService('ServerStorage').Value.Value
function module.GetQuestions()
local ret = ""
local function request()
-- Remember to set enable HTTP Requests in game settings!
local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync(
{
Url = "https://opentdb.com/api.php?amount=10&difficulty=easy&type=multiple", -- This website helps debug HTTP requests
Method = "GET",
Headers = {
["Content-Type"] = "application/json" -- When sending JSON, set this!
},
}
)
-- Inspect the response table
if response.Success then
print("Status code:", response.StatusCode, response.StatusMessage)
local test = HttpService:JSONDecode(response.Body)
warn(test)
module.question = test["results"][tostring(module.sers)]
module.WrongAnswers = test["results"][module.sers]["incorrect_answers"]
module.CorrectAnswer = test["results"][module.sers]["correct_answer"]
module.Category = test["results"][module.sers]["General Knowledge"]
module.Difficulty = test["results"][module.sers]["difficulty"]
else
print("The request failed:", response.StatusCode, response.StatusMessage)
end
end
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
print("Http Request failed:", message)
return nil
end
end
return module
and main script
\
local repf = game:GetService("ReplicatedFirst")
local Quesmodule = require(repf.Question)
local Current = 1
local function PickQuestion()
warn(Quesmodule.test)
local players = game.Players:GetPlayers()
local player = players[1]
local Gui = player.PlayerGui:WaitForChild("ScreenGui").Frame
local t = {}
for i,v in pairs(Quesmodule.WrongAnswers) do
table.insert(t,v)
end
table.insert(t,#t+1,Quesmodule.CorrectAnswer)
--How do I Randomize a table?
Gui.TextLabel.Text = Quesmodule.question
Gui.TextButton1.Text = t[1]
Gui.TextButton2.Text = t[2]
Gui.TextButton3.Text = t[3]
Gui.TextButton4.Text = t[4]
game.ReplicatedStorage.AnswerChosen.OnServerEvent:Connect(function(player,chosenAnswer)
if chosenAnswer == Quesmodule.CorrectAnswer then
print("Yay.")
wait("1")
PickQuestion()
else
print("Wrong")
wait("1")
end
end)
wait(.5)
end
game.Players.PlayerAdded:Connect(function (player)
player.CharacterAdded:Wait()
PickQuestion()
print("yes")
end)
Here is a screen shot https://gyazo.com/0d08736a9b6c92d36a14e1abf177b9a7