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

result:FireAllClients() does not work after some testing. I tried this part of a script just to test a theory of mine and it did not work either, proving my theory.

	local test = game.ReplicatedStorage.Minigames:GetChildren()
	test:FireAllClients()

Even though the child is a remote event firing all clients on the child of Minigame does not work.
I’ll likely need some way to find out what result is each time and make a direct reference to it like local test2 = game.ReplicatedStorage.Minigames["Ball Race"].

1 Like

In that script, the variable test is not a single event, it’s a list. GetChildren() returns a list of children, FindFirstChild() returns a single child.

I’ve set up a basic test on my end. It’s three things:

  1. Server script:
wait(5) --give the client time to load before we fire it

game.ReplicatedStorage.Minigames:FindFirstChild("Ball Race"):FireAllClients()
  1. Client script:
local event = game.ReplicatedStorage.Minigames["Ball Race"]

local function Trigger()
	print("It worked!")
end

event.OnClientEvent:Connect(Trigger)

image

It worked for me, so using FindFirstChild with FireAllClients works fine.

2 Likes

I don’t know why result:FireAllClients() isn’t working for me still then.

1 Like

No matter what I do nothing works. It won’t print “It worked!”. I’ve tried game.ReplicatedStorage.Minigames:FindFirstChild("Ball Race"):FireAllClients() but it doesn’t work.

1 Like

I’d suggest stepping through the things your script needs to do one at a time, with a bunch of print statements for good measure. Have your code print randomGame.Name, see what it’s searching for.

Also, make sure you give your client enough time to load before you fire your test event. I consider 3 seconds to be safe, but usually allow 5 just in case Roblox feels like being slow on that test run.

3 Likes

I added some time for it to wait and now it’s printing. I’ll test more until I get back to the original script or until I run into an error.

1 Like

Any way I can make it so that a normal script can detect when the RemoteEvent is fired? I might need it later.

1 Like

Taking a look at your script, you don’t need to use :FindFirstChild() to find the remote event, as you have already gotten it when defining the randomGame variable. You should be able to do:

randomGame:FireAllClients()

Please don’t do this, do: game.ReplicatedStorage.Minigames:WaitForChild(“Ball Race”) instead. This makes sure the remote event is ready at the minimum time delay.

I also highly do not recommend putting spaces in names as this makes it much more complicated to get instances in scripts.

1 Like

If I were to continue to use RandomGame then it would be random every time, which would be a problem. I added a space because I’m setting the name of it to the text of a text label. The whole idea is that it chooses a random option and then fires the right remote event.

1 Like

No, this isn’t always the case, the way variables work is they return an instance instead of redoing the function. This means the returned variable will always return the same until it is defined again. For example:

local number = math.random(1,1000)
print(number) --974
print(number) --974
number = math.random(1,1000) --redefining the variable
print(number) --275
2 Likes

That won’t work. Waiting for the child to load on the server side does not guarantee that the client has loaded, which is where the print statement we’re using to debug comes from. If you care about it, you can wait for a player to join and then fire the event, but I wanted something that was simple and removed all possible sources of error.

3 Likes

Apologies, I did not fully understand the topic. If you want it to wait until the client has responded, you can use a remote function. Remote Functions are different as they do wait for a response unlike remotevents.

3 Likes

Note that if you use this method, you can’t enjoy the privileges of :FireAllClients. You can work around this by doing:

for i, v in pairs (game.Players:GetPlayers()) do
event:InvokeClient(v)
end
2 Likes

This is all very confusing. I don’t even know what to mark as the solution to this.

1 Like

A solution is not needed for anything, just an indicator for other developers that the topic no longer needs support. If you absolutely want a solution, what was the final reply that helped you solve the problem?

2 Likes
local other_folder_name = otherFolder.Name
for i,v in ipairs(folder:GetChildren()) do
  if v.Name == other_folder_name then
    return v
  end
end
2 Likes

I tried that with this script:

local votingOption = game.Workspace.VotingSpace1.VotingOption1
local folder = game.ReplicatedStorage.Minigames

local folderName = votingOption.Name
task.wait(5)
for i,v in ipairs(folder:GetChildren()) do
	if v.Name == folderName then
		print("Test worked")
		return v
	end
end

It didn’t end up working. I don’t know if this would be because the name of the folder changes.

I just realized that this script…

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

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

…hasn’t beeen printing “It worked!”.

you seems to be using multiple events for the voting system . why not change it to one? you can just use one to fire from server to client to tell the client what choice it have . then you can use the same event for sending the name of the map that the client chose …

1 Like

It’s not that I’m lazy (clearly) … It’s that I gage my chance of answering questions by the information provided. If I don’t feel there is enough information. I really don’t want to put people through my speculation. I do have a very wild imagination :eyes:

1 Like