How to fix Output printing function 3 times in a row?

I have a Local script that fires a Players name in a remote event, and then a server script that gets the Players name and prints it in the output. For some reason though, if there are any more than 1 player in the server, it prints every name 3 times.

Here’s the scripts + what I see in the output

Below is the Local Script, it first intercepts a value from another script that fires a folder that has each player within it, (value)

game.ReplicatedStorage.TestEvent.OnClientEvent:Connect(function(value)
if #value >= 1 then
		for i , v in ipairs(value) do
		Names = v.Name	
		local player= game.Players:GetChildren(Names)
		game.ReplicatedStorage.PrintName:FireServer(Names)
    end
end)

and heres the server script that recieves it:

game.ReplicatedStorage.PrintName.OnServerEvent:Connect(function(PlayerNames)
	print(PlayerNames)
end)

When I do this by myself it only prints my name, but when I do it in a local Server in studio with 2 players, it does this

Screen Shot 2022-07-28 at 1.17.05 PM

I only want it to print each player once. How do I fix this?

I’m not sure if that works, use :FindFirstChild(Names)

Also, how is the TestEvent getting fired? If it is using FireAllClients then that is why, if not then show the code that fires the TestEvent

PlayerNames would be the player that fired the event. You need to add a variable for that.

game.ReplicatedStorage.PrintName.OnServerEvent:Connect(function(Player, PlayerNames)

Also, you need to use FindFirstChild instead of GetChildren as @domboss37 says.

Yes, but it would still print out the players name.

Change this

game.ReplicatedStorage.PrintName.OnServerEvent:Connect(function(PlayerNames)

To

game.ReplicatedStorage.PrintName.OnServerEvent:Connect(function(Player , PlayerNames)

This is your script with everything corrected and comments
how is your client event fired?

game.ReplicatedStorage.TestEvent.OnClientEvent:Connect(function(value)
	--if #value >= 1 then   -- you don't need to check the number it will only loop if values has atleast 1
	local Names = {}   -- setup empty table to hold the names
	for i , v in ipairs(value) do  -- loop through the values table
		Names[i] = v.Name  -- use i as key set name here	
		local player= game.Players:FindFirstChild(v.Name)  -- not sure why you are gettting player but this is how you would find it by name from value table
	end
		game.ReplicatedStorage.PrintName:FireServer(Names)  -- send the Names table to server   I would think this needs to go outside of the for loop when it finishes the names table
end)
game.ReplicatedStorage.PrintName.OnServerEvent:Connect(function(Player, PlayerNames)  -- as above need player to be first varaible on server always in event
	print(PlayerNames)  -- print names table from client
end)

Edited the fire server to be outside the for loop after setting up the full names table

I implemented your code and got an interesting output and not sure why

It’s still repeating more than once, I only want it to print this table a single time

Screen Shot 2022-07-28 at 2.02.32 PM

Also to answer your question, the Client event was fired by this

local value = game.Workspace.PlayersAlive:GetChildren()
game.ReplicatedStorage.TestEvent:FireClient(player, value)

I had originally written FireAllClients, but I fixed it, still makes it repeat though.

is this inside of a for loop for multi players or something?

Yeah its inside of this for loop

for i,player in pairs(game.Players:GetChildren()) do
		local value = game.Workspace.PlayersLeft:GetChildren()
		game.ReplicatedStorage.TestEvent:FireClient(player, value)

Should I change something here?

well for every player that is in the game its going to fire the server event with that table

is this different here from the above where it says players alive is there like 2 loops or you just rewrote part of this?

It shouldn’t be in a loop, u r sending all the players for every player / loop

My bad I just rewrote the event part

One more question, if I move this

local value = game.Workspace.PlayersLeft:GetChildren()
game.ReplicatedStorage.TestEvent:FireClient(player, value)	

out of the loop, how do I get player? Currently it says “unknown Global ‘player’”

To get the player use game.Players.LocalPlayer
Since this is a local script

I’m firing the client from the server

Oh right, can i see the full code or something?

And here

for i,player in pairs(game.Players:GetPlayers())

The “player” will be the player Instance, use it when firing then

the player will be automatically set in the OnServerEvent once when you fire the remote for ex :

--Client
game.ReplicatedStorage.TestEvent:FireClient()
-- Server
game.ReplicatedStorage.TestEvent.OnServerEvent:Connect(function(plr)
print(plr.Name)
end)
1 Like

you said you’re firing the client from the server,then that works

local player = game.Players.LocalPlayer
local value = game.Workspace.PlayersLeft:GetChildren()
game.ReplicatedStorage.TestEvent:FireClient(player, value)

in the server script what is the reason for them checking the names is it like a round ending or something?

this is what should determine how you fire the clients

Yeah its when the round ends

A BoolValue is in each player that determines whether they died during the round or not, and if they didn’t die the event fires. (Also, the PlayersLeft in workspace is a folder that houses each players string value. If they die during the round it gets removed.)

if Dead.value == false and InRound.Value == false then
	for i,player in pairs(game.Players:GetPlayers()) do
	local value = game.Workspace.PlayersLeft:GetChildren()
   game.ReplicatedStorage.TestEvent:FireClient(player, value)	

That code is what I have right now, basically it works for me if I’m the only player, but when I do the local test with 2 players it prints out the winner 4 times in a row. It’s not the same as it was before, as it only prints the winner instead of all players, but it still isn’t only printing once.