How to compare 1 name to every child of something and have the matching one be referenced later in the script

Right now I’m trying to make a voting system and I’m working my way there step by step.

Explanation Of The Script

Right now what I’m doing is choosing a random option from a folder’s children and then setting a folder’s name to the chosen option’s name. Next I’m setting a some text to match the name of the folder.

local textLabel = script.Parent.SurfaceGui.Frame.TextLabel
local games = game.ReplicatedStorage.Minigames:GetChildren()
local randomGame = games[math.random(1, #games)]
local option = game.Workspace.VotingSpace1.VotingOption1

option.Name = randomGame.Name
textLabel.Text = option.Name
-- Something here that finds a child that has a matching name as "option" from "games" and is able to refrence it later

What I need to do is from a folder’s children find one of them that matches the name of the other folder.
I searched for solutions here but they seemed vague and didn’t help.

5 Likes

Something is wrong with your logic here … this works fine.
Need to upload more than this for a help testing. I don’t have your voting menu.
I’m not going to guess re-wright this out. Create a mock script/menu for testing and upload that.

task.wait(3)

local games = game.ReplicatedStorage.Minigames:GetChildren()
local randomGame = games[math.random(1, #games)]
--local option = game.Workspace.VotingSpace1.VotingOption1

print(randomGame.Name)

--option.Name = randomGame.Name
--randomGame.Text = option.Name
4 Likes

My current script works fine, it’s just that I need something else added on to it but I don’t know how to do it and I couldn’t find anything that told me how to do it.

4 Likes

Again … I have no clue what your menu looks like or how it’s put together. I’m also lazy, make it easy to help you.

3 Likes

If I’m understanding your question right, you’re looking for a way to get a reference to an object in a folder with a name that matches a search query. You can do that easily with FindFirstChild, but you have to handle the possibility that FindFirstChild won’t find what you’re looking for, in which case it will return nil.

local games = game.ReplicatedStorage.Minigames:GetChildren()
local randomGame = games[math.random(1, #games)]
local option = game.Workspace.VotingSpace1.VotingOption1

option.Name = randomGame.Name
textLabel.Text = option.Name
local result = games:FindFirstChild(randomGame.Name)
if not result then
    warn("Game not found!")
else
    --whatever other code logic you're working with
end

That’s code I wrote without really checking it, so it’s possible I have some errors in there somewhere. It may not just drag-and-drop in.

3 Likes

This takes care of almost everything. The children are remote events and I need them to be fired. I don’t know if something like result:FireAllClients() would work.

2 Likes

You should be able to fire clients from the else block, yes. I based the search on the randomGame, which I’m sot sure is correct based on what you’re going for, but it helped demonstrate FindFirstChild.

The other thing to be careful of is running FindFirstChild across client-server replication, since it can fail if the child you’re looking for hasn’t been replicated yet. If that’s an issue, you can use WaitForChild instead.

2 Likes

I tested it and it did not appear to work. I might have done something wrong with the script I used to test it with. I removed the other option by the way. (There were 2 options)
This is the script I used to test it:

local event = game.ReplicatedStorage.Minigames["Ball Race"]

local function Trigger()
	event.OnServerEvent:Connect()
	task.wait(1)
	print("It worked!")
end

The script is a normal script under ServerScriptService.

2 Likes

If that’s the whole script, then you aren’t connecting your Trigger function to anything. Unless the Connect() inside the function is a trick I don’t know about, that function will never run. Try it this way.

local event = game.ReplicatedStorage.Minigames["Ball Race"]

local function Trigger()
	task.wait(1)
	print("It worked!")
end

event.OnServerEvent:Connect(Trigger)
3 Likes

I was trying to figure out how to change one of my friend’s scripts to work for me. I tried the script now and it still didn’t work. Also if you couldn’t tell already I’m not much of a scripter, considering the mistakes I’ve made. At least with each mistake and solution I learn a bit more.

2 Likes

you can just take

local RemoteEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("MessageEvent")
function Trigger()
RemoteEvent:FireAllClient(--put message here like--) minigame.Name .. " is choosem"
2 Likes

If you need any help, you can add me to the game so I can help you debug

2 Likes

The script you provided does not help. It appears to do something different than what I’m trying to do. (I feel like this sounds rude, but I’m not trying to be mean.)

2 Likes

sorry for the confusion . What script you need? What problems are you facing, with this answers, I can help you easily

2 Likes

What I’m trying to do right now is find out how to fire the original RemoteEvent that was randomly chosen.
This is the script that I have right now:

local textLabel = script.Parent.SurfaceGui.Frame.TextLabel
local games = game.ReplicatedStorage.Minigames:GetChildren()
local randomGame = games[math.random(1, #games)]
local option = game.Workspace.VotingSpace1.VotingOption1

option.Name = randomGame.Name
textLabel.Text = option.Name
local result = game.ReplicatedStorage.Minigames:FindFirstChild(option.Name)
if not result then
	warn("Did not work!")
else
	result:FireAllClients() -- This part doesn't work.
end

This is the script I’m using to test it:

local event = game.ReplicatedStorage.Minigames["Ball Race"]

local function Trigger()
	task.wait(1)
	print("It worked!")
end

event.OnServerEvent:Connect(Trigger)
2 Likes

Ok, so I missed something that’s messing you up.

Remote Events have two parts. Server and client. You can fire the server from a client, or you can fire a client (or all clients) from the server. You can’t fire the server from the server, and the server cannot see client events.

Your tester is looking for a server event, which is what happens when a client calls FireServer(). You need OnClientEvent, which is what happens when a server calls FireClient() or FireAllClients().

Your tester needs to be a LocalScript under StarterPlayer.StarterPlayerScripts, and it will need this modification.

local event = game.ReplicatedStorage.Minigames["Ball Race"]

local function Trigger()
	task.wait(1)
	print("It worked!")
end

event.OnClientEvent:Connect(Trigger)
3 Likes

I don’t think that is possible but instead you could send over the result and on the local script check if result is equal to what ever you’re checking for, then add your logic after.

2 Likes

It didn’t seem to work with OnClientEvent either.

2 Likes

Is it in a LocalScript inside StarterPlayerScripts? OnClientEvent doesn’t work in a server-side script.

3 Likes

It is a LocalScript inside StarterPlayerScripts.

1 Like