Why does this remote fire twice, and only twice?

I want this remotevent to fire only once(which I’ll get to eventually), but currently I’m interested in how come it fires exactly twice, and no more than that. I don’t understand why it’d fire twice for each player(I tested with another person) if the number of players is the cause , as it’s a serverscript.
P.S: forgot to add the screenshot of the client remoteevent printing “Hello” twice as well.

local presidenteTeam = game.Teams["El presidente"]
local civilian = game.Teams.Civilian
local Players = game:GetService("Players")
local votec = game.ReplicatedStorage:FindFirstChild("voteclient")
local sound = Instance.new("Sound", workspace)

while true do 
	wait(4)
	if #presidenteTeam:GetPlayers() == 0 then
		if #civilian:GetPlayers() > 1 then
			print("election")
			sound.SoundId = "rbxassetid://9071337529"
			sound:Play()
			votec:FireAllClients()
			sound.Ended:Wait()
			sound:Destroy()
		
		end
	end
end

Capture

You are using while true, which runs constantly nonstop. Also you are going to use FireAllClients when you are using an if statement for if there is more than one person in the server.

You have it wrapped in a while loop and waiting 4 seconds and this is the reason why its printing twice because if you look at the time stamp you can see the 4 second difference

Can’t you use FireClient(PlayerObject) instead? You’re using FireAllClients(), which is the function’s main purpose of what it’s used for

You put that event under a while loop, which will apparently run forever.

I know how while true works. I just don’t get why it does it only twice, instead of hundreds of times more.

I believe this is because you are destroying the sound in the first loop, leading to it printing “election” twice and erroring here

The while loop would stop because of this error. So to fix this, create the sound within the loop.

local presidenteTeam = game.Teams["El presidente"]
local civilian = game.Teams.Civilian
local Players = game:GetService("Players")
local votec = game.ReplicatedStorage:FindFirstChild("voteclient")

while true do 
	wait(4)
	if #presidenteTeam:GetPlayers() == 0 then
		if #civilian:GetPlayers() > 1 then
			print("election")
			local sound = Instance.new("Sound", workspace)
			sound.SoundId = "rbxassetid://9071337529"
			sound:Play()
			votec:FireAllClients()
			sound.Ended:Wait()
			sound:Destroy()
		
		end
	end
end
1 Like

It appears that was the issue! Thank you, and now I’ll try to limit the remoteevent from firing lol

The remote event wouldn’t be a problem here. That would only be a problem if you were firing it more than 10 times a second (this is a rough estimation, but it probably takes way more than that).

What I meant was “I need to limit the remoteevent to fit how I want it to work for my game”

Oh, sorry, I misinterpreted what you meant. :sweat_smile: