我是一名小型 roblox 开发者,我遇到了一个问题。 现在,我为问答游戏制作了一个本地脚本,该游戏从数据中提出问题,用户必须在一定时间内回答问题(如图所示)
我要解决的问题是,目前游戏仅适用于开始测验的个人,而不适用于其他人
但我不知道如何修改我的脚本,以便每个人同时有相同的问题。
我尝试学习 RemoteEvent 策略,但经过 5 个小时的反复尝试,它从未奏效
这是现在测验的代码
local trivialevel1 = {
{ --Math
question = "What is the mathematical constant π (pi) approximately equal to?",
answers = {"2.718","3.142","1.618","0.577"},
correctanswer = 2,
category = "Math"
},
{
question = "What is the result of multiplying any number by zero?",
answers = {"1","The number itself","Zero","Infinity"},
correctanswer = 3,
category = "Math"
},
{
question = "What is the term for a polygon with five sides?",
answers = {"Hexagon","Octagon","Pentagon","Heptagon"},
correctanswer = 3,
category = "Math"
},
{
question = "What is the value of the square root of 144?",
answers = {"144","16","9","12"},
correctanswer = 4,
category = "Math"
},
{
question = "What is the sum of all angles in a triangle?",
answers = {"270","180","360","90"},
correctanswer = 2,
category = "Math"
},
{
question = "If a number is divisible by 2 and 3, what other number is it also divisible by?",
answers = {"4","8","6","5"},
correctanswer = 3,
category = "Math"
},
{ --Sports
question = "Which country won the most recent FIFA World Cup in 2022?",
answers = {"Brazil","Germany","France","Argentina"},
correctanswer = 4,
category = "Sports"
},
{
question = "Which sport is associated with the term 'slam dunk'?",
answers = {"Soccer","Tennis","Baseball","Basketball"},
correctanswer = 4,
category = "Sports"
},
{
question = "In baseball, how many strikes constitute a strikeout for a batter?",
answers = {"4","2","3","1"},
correctanswer = 3,
category = "Sports"
},
{ --Geography
question = "What is the world's largest ocean?",
answers = {"Atlantic Ocean","Idian Ocean","Artic Ocean","Pacific Ocean"},
correctanswer = 4,
category = "Geography"
},
{
question = "What is the capital city of Japan?",
answers = {"Beijing","Seoul","Tokyo","Bangkok"},
correctanswer = 3,
category = "Geography"
},
{
question = "What is the largest country in Africa by land area?",
answers = {"Nigeria","Egypt","South Africa","Algeria"},
correctanswer = 4,
category = "Geography"
}
}
local trivialevel2 = {
{ --Math
question = "Which of the following is the smallest prime number?",
answers = {"3","2","1","4"},
correctanswer = 2,
category = "Math"
},
{
question = "In the equation E=mc², what does the 'c' represent?",
answers = {"Speed of light", "Planck constant","Gravitational constant","Electric charge"},
correctanswer = 1,
category = "Math"
},
{ --Sports
question = "Which athelete is often referred to as The Fastest Man on Earth?",
answers = {"Michael Phelps","Usain Bolt","LeBron James","Cristiano Ronaldo"},
correctanswer = 2,
category = "Sports"
},
{
question = "Which Winter Olympic sport involves atheletes sliding down a frozen track on a sled?",
answers = {"Ice hockey","Curling","Ski jumping","Skeleton"},
correctanswer = 4,
category = "Sports"
},
{
question = "In American football, how many points is a safety worth? ",
answers = {"2","3","6","1"},
correctanswer = 1,
category = "Sports"
},
{
question = "Which tennis player has won the most Wimbledon titles in the men's singles category?",
answers = {"Andre Agassi","Rafael Nadal","Roger Federer","Novak Djokovic"},
correctanswer = 3,
category = "Sports"
},
{ --Geography
question = "Which continent is home to the Sahara Desert, the largest hot desert in the world?",
answers = {"South America","Africa","Asia","Australia"},
correctanswer = 2,
category = "Geography"
},
{
question = "Which river is the longest in the world?",
answers = {"Nile","Amazon","Yangtze","Mississippi"},
correctanswer = 1,
category = "Geography"
},
{
question = "Which European country is known as the 'Land of Fire and Ice' due to its volcanic activity and glaciers?",
answers = {"Sweden","Finland","Iceland","Norway"},
correctanswer = 3,
category = "Geography"
},
{ --Food
question = "Which Italian pasta is shaped like small rice grains?",
answers = {"Penne","Fusilli","Orzo","Linguine"},
correctanswer = 3,
category = "Food"
},
}
local trivialevel3 = {
{ --Math
question = "Which famous mathematician is known for his work on the theory of relativity?",
answers = {"Isaac Newton","Galileo Galilei","Albert Einstein","Archimedes"},
correctanswer = 3,
category = "Math"
},
{ --Sports
question = "What is the national sport of Japan?",
answers = {"Sumo wrestling","Judo","Karate","Kendo"},
correctanswer = 1,
category = "Sports"
},
{
question = "Which gold major is held at Augusta National Golf Club?",
answers = {"The Open Championship","The PGA Championship","The Masters Tournament","The U.S Open"},
correctanswer = 3,
category = "Sports"
},
{
question = "Which country is known for dominating the sport of cricket and is considered a cricket powerhouse?",
answers = {"England","Australia","South Africa","India"},
correctanswer = 4,
category = "Sports"
},
{ --Geography
question = "The Great Barrier Reef, one of the world's most famous coral reef systems, is located off the coast of which country?",
answers = {"Brazil","Australia","Indonesia","Mexico"},
correctanswer = 2,
category = "Geography"
},
{
question = "Which mountain range stretches across seven countries in South America?",
answers = {"Rocky Mountains","Andes","Alps","Himalayas"},
correctanswer = 2,
category = "Geography"
},
{
question = "Which famous canal connects the Mediterranean Sea to the Red Sea?",
answers = {"Suez Canal","Panama Canal","Kiel Canal","Corinth Canal"},
correctanswer = 1,
category = "Geography"
},
{
question = "What is the smallest independent country in the world, both in terms of area and population?",
answers = {"Monaco","Vatican City","Liechtenstein","San Marino"},
correctanswer = 2,
category = "Geography"
},
}
local question = script.Parent.Parent.Parent.Quiz.Frame.Question
local answer1 = script.Parent.Parent.Parent.Quiz.Frame.Answer1
local answer2 = script.Parent.Parent.Parent.Quiz.Frame.Answer2
local answer3 = script.Parent.Parent.Parent.Quiz.Frame.Answer3
local answer4 = script.Parent.Parent.Parent.Quiz.Frame.Answer4
local category = script.Parent.Parent.Parent.Quiz.Frame.Category
local currentquestionindex = 1
local selectedIndex = 0
local buttons = {answer1,answer2,answer3,answer4}
local player = game.Players.LocalPlayer
--TIMER
local timer = script.Parent.Parent.Parent.Quiz.Frame.Timer
local countdownDuration = 10
local currentCountdown = countdownDuration
local isCounting = false
local function updateTimer()
while isCounting and currentCountdown > 0 do
timer.Text = tostring(currentCountdown)
wait(1)
currentCountdown = currentCountdown - 1
end
timer.Text = "Time's up!"
end
local function startCountdown()
isCounting = true
currentCountdown = countdownDuration
updateTimer()
end
local function stopCountdown()
isCounting = false
end
local function shuffle(array)
local n = #array
for i = n, 2, -1 do
local j = math.random(i)
array[i], array[j] = array[j], array[i]
end
end
local function displayQuestionlevel1(index)
local questionData = trivialevel1[index]
question.Text = questionData.question
end
local function displayQuestionlevel2(index)
local questionData = trivialevel2[index]
question.Text = questionData.question
end
local function displayQuestionlevel3(index)
local questionData = trivialevel3[index]
question.Text = questionData.question
end
local function displayAnswerslevel1(index)
local answersdata = trivialevel1[index]
answer1.Text = answersdata.answers[1]
answer2.Text = answersdata.answers[2]
answer3.Text = answersdata.answers[3]
answer4.Text = answersdata.answers[4]
end
local function displayAnswerslevel2(index)
local answersdata = trivialevel2[index]
answer1.Text = answersdata.answers[1]
answer2.Text = answersdata.answers[2]
answer3.Text = answersdata.answers[3]
answer4.Text = answersdata.answers[4]
end
local function displayAnswerslevel3(index)
local answersdata = trivialevel3[index]
answer1.Text = answersdata.answers[1]
answer2.Text = answersdata.answers[2]
answer3.Text = answersdata.answers[3]
answer4.Text = answersdata.answers[4]
end
local function displayCategorylevel1(index)
local categorydata = trivialevel1[index]
category.Text = categorydata.category
end
local function displayCategorylevel2(index)
local categorydata = trivialevel2[index]
category.Text = categorydata.category
end
local function displayCategorylevel3(index)
local categorydata = trivialevel3[index]
category.Text = categorydata.category
end
local function addCoins(amount)
player:WaitForChild("leaderstats"):WaitForChild("Coins").Value = player.leaderstats.Coins.Value + amount
end
script.Parent.Parent.Parent.Quiz.bouton.MouseButton1Click:Connect(function()
local error1 = script.Parent.Parent.Parent.Quiz.Frame.Error1
local error2 = script.Parent.Parent.Parent.Quiz.Frame.Error2
local error3 = script.Parent.Parent.Parent.Quiz.Frame.Error3
error1.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
error2.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
error3.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
script.Parent.Parent.Parent.Quiz.Frame.Visible = true
local questionsdone = 0
local errorCount = 0
while(questionsdone < 7) do
script.Parent.Parent.Parent.Quiz.Frame.Answer1.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer2.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer3.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer4.BackgroundColor3 = Color3.fromRGB(255,122,69)
shuffle(trivialevel1)
displayQuestionlevel1(currentquestionindex)
displayAnswerslevel1(currentquestionindex)
displayCategorylevel1(currentquestionindex)
startCountdown()
if selectedIndex == trivialevel1[currentquestionindex].correctanswer then
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
addCoins(10)
else
buttons[trivialevel1[currentquestionindex].correctanswer].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(255,0,0)
errorCount = errorCount + 1
if errorCount == 1 then
error1.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 2 then
error2.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 3 then
error3.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
-- Code pour le personnage qui meurt ici (ajoutez votre propre logique)
game.Players.LocalPlayer.Character:BreakJoints()
end
end
wait(5)
questionsdone = questionsdone + 1
end
while(questionsdone < 14) do
script.Parent.Parent.Parent.Quiz.Frame.Answer1.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer2.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer3.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer4.BackgroundColor3 = Color3.fromRGB(255,122,69)
shuffle(trivialevel2)
displayQuestionlevel2(currentquestionindex)
displayAnswerslevel2(currentquestionindex)
displayCategorylevel2(currentquestionindex)
startCountdown()
if selectedIndex == trivialevel2[currentquestionindex].correctanswer then
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
addCoins(10)
else
buttons[trivialevel2[currentquestionindex].correctanswer].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(255,0,0)
errorCount = errorCount + 1
if errorCount == 1 then
error1.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 2 then
error2.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 3 then
error3.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
-- Code pour le personnage qui meurt ici (ajoutez votre propre logique)
game.Players.LocalPlayer.Character:BreakJoints()
end
end
wait(5)
questionsdone = questionsdone + 1
end
while(nil) do
script.Parent.Parent.Parent.Quiz.Frame.Answer1.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer2.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer3.BackgroundColor3 = Color3.fromRGB(255,122,69)
script.Parent.Parent.Parent.Quiz.Frame.Answer4.BackgroundColor3 = Color3.fromRGB(255,122,69)
shuffle(trivialevel3)
displayQuestionlevel3(currentquestionindex)
displayAnswerslevel3(currentquestionindex)
displayCategorylevel3(currentquestionindex)
startCountdown()
if selectedIndex == trivialevel3[currentquestionindex].correctanswer then
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
addCoins(10)
else
buttons[trivialevel3[currentquestionindex].correctanswer].BackgroundColor3 = Color3.fromRGB(35, 255, 19)
buttons[selectedIndex].BackgroundColor3 = Color3.fromRGB(255,0,0)
errorCount = errorCount + 1
if errorCount == 1 then
error1.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 2 then
error2.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
elseif errorCount == 3 then
error3.BackgroundColor3 = Color3.fromRGB(255, 0, 0) -- Rouge
-- Code pour le personnage qui meurt ici (ajoutez votre propre logique)
game.Players.LocalPlayer.Character:BreakJoints()
end
end
wait(5)
questionsdone = questionsdone + 1
end
end)
script.Parent.Parent.Parent.Quiz.Frame.Answer1.MouseButton1Click:Connect(function()
selectedIndex = 1
script.Parent.Parent.Parent.Quiz.Frame.Answer1.BackgroundColor3 = Color3.fromRGB(143,68,39)
script.Parent.Parent.Parent.Quiz.Frame.Answer1.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer2.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer3.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer4.Active = false
end)
script.Parent.Parent.Parent.Quiz.Frame.Answer2.MouseButton1Click:Connect(function()
selectedIndex = 2
script.Parent.Parent.Parent.Quiz.Frame.Answer2.BackgroundColor3 = Color3.fromRGB(143,68,39)
script.Parent.Parent.Parent.Quiz.Frame.Answer1.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer2.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer3.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer4.Active = false
end)
script.Parent.Parent.Parent.Quiz.Frame.Answer3.MouseButton1Click:Connect(function()
selectedIndex = 3
script.Parent.Parent.Parent.Quiz.Frame.Answer3.BackgroundColor3 = Color3.fromRGB(143,68,39)
script.Parent.Parent.Parent.Quiz.Frame.Answer1.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer2.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer3.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer4.Active = false
end)
script.Parent.Parent.Parent.Quiz.Frame.Answer4.MouseButton1Click:Connect(function()
selectedIndex = 4
script.Parent.Parent.Parent.Quiz.Frame.Answer4.BackgroundColor3 = Color3.fromRGB(143,68,39)
script.Parent.Parent.Parent.Quiz.Frame.Answer1.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer2.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer3.Active = false
script.Parent.Parent.Parent.Quiz.Frame.Answer4.Active = false
end)
感谢你们给我的任何帮助:)
我可以为您提供解决我的问题所需的任何其他信息
解决此问题最直接的方法是在服务器端选择问题和答案集,然后使用
RemoteEvent
将信息发送到使用 FireAllClients()
的所有客户端,然后让客户端发送返回一个号码,详细说明他们使用 FireServer()
选择的号码
完整的流程如下:
服务器选择问题/答案集
(serverscript)
→
服务器将问题/答案集发送给客户端 (RemoteEvent:FireAllClients())
→
客户接收并显示问题/答案集 (localscript)
→
玩家选择→客户端将信息发送回服务器(RemoteEvent:FireServer())
→服务器跟踪谁回答了什么,并奖励积分或任何你想做的事情(serverscript)
只要您的 RemoteEvent 能够在 ReplicatedStorage 中引用,服务器脚本和本地脚本都可以通过您通过 RemoteEvent 提供的数据相互通信。