Casting GUI's text to every single player

Hello,

I have made a code which needs to randomize questions and the question that has been selected has text in it and that specific text needs to be in the textlabel and textbutton.

But I don’t know how I can put the text that needs to that question and put it into the textlabel and textbutton.

Here’s the script and maybe that will clearify what I mean:

local A = script.Parent.Parent.A
local B = script.Parent.Parent.B
local C = script.Parent.Parent.C
local D = script.Parent.Parent.D



local question =  script.Parent

local question1 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},
		
		correct = {1}
		
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = { 
			content = "",
		}
		
	}
}

local question2 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},
		
		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}


local question3 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},

		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}

local question4 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},

		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}

local question5 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},

		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}

local question6 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},

		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}

local question7 = {
	[0] = {
		content = "",
		answers = {1,2,3,4},

		correct = {1}
	},
	answers = {
		[1] = {
			content = "",
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = {
			content = "",
		}
	}
}



1 Like

You’re looking for RemoteEvent:FireAllClients()

2 Likes

You can use pair loops with the get children. Here’s a format for example:

for i, v in pairs(game.Players:GetChildren()) do
    v.PlayerGui:WaitForChild("TEST").Text = "test1"

I’m not really sure what you tried to do but this is an example. A pair loop with :GetChildren() Basically runs
over all the children in the specified service / instance which in this case the player service
would take all the players ingame and apply the modifications.
Very efficient way to do so.

Hopefully I understood you correctly and this has helped you

Thanks this will be very helpfull so every player gets the same question with their answers. But… we’re not there yet.

Here is the next step:

local question1 = {
	[0] = {
		content = "", -- Text that defines the question
		answers = {1,2,3,4},
		
		correct = {1}
		
	},
	answers = {
		[1] = {
			content = "", -- Text that defines the answer
		},
		[2] = {
			content = "",
		},
		[3] = {
			content = "",
		},
		[4] = { 
			content = "",
		}
		
	}
}

In my code there are questions that are named question1, question2 and so on. And to make a question they need to get randomized so the script chooses on of the questions.

Then there are a TextLabel where the question’ text needs to get put in and TextButtons for the answers.
Ofcourse the TextLabel and the TextButtons need to have text in it.

  • Question’ text is marked in the script.
  • Answers’ text is marked in the script.

Each answer has a number as shown in the code. In this case number 1 will always be the correct answer. That is also indicated in the code.

So for example answer A has the text(content) and I only need to know how I can get the randomized question and the question’ and the answers’ content into the TextLabel and TextButtons.

Hope this was a bit more understandable.

Questions = {
[1] = {}
[2] = {}
}

You can use-
local n = math.random(1,#Questions)
Questions[n]
-to randomly choose a question.

You can insert all the questions (question1, question2, …) into an array, and then pick a random element from the array with array[math.random(#array)]

Then you can use a remote event to tell all the clients what the random question you picked was with remote:FireAllClients(question, {answer1, answer2, answer3, answer4})

Then on the client you can listen to the remote call from the server with remote.OnClientEvent and set the text of the question and answer choices on the client’s screen to whatever was passed to OnClientEvent

Then have the client fire the remote event when they click on a button with the answer with remote:FireServer(answerChoice)

Then have the server listen to the client’s remote call with remote.OnServerEvent and check if the client’s answer matches the randomly picked question’s correct answer

You should also probably put this script in ServerScriptService and not a separate copy in everyone’s gui