RemoteEvent Won't fire to client!

Hello!

So I’m making a Flood Survival game and It’s managed by RemoteEvents.

But the problem Is, when the timer is to 0, the script should fire to the client. And to make sure It Isn’t broken, I’ve made a short code. And It Is broken!

Here’s the code for both scripts: (BOTH OF THE SCRIPTS ARE WIP!)

Server Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownEvent = ReplicatedStorage:WaitForChild("CountdownRemoteEvent")
local MainEvent = ReplicatedStorage:WaitForChild("MainEvent")
local Status = ReplicatedStorage:WaitForChild("Status")
local InRound = ReplicatedStorage:WaitForChild("InRound")
local Players = game:GetService("Players")

for t = Status.Value, 1, -1 do
	CountdownEvent:FireAllClients(t)
	Status.Value -= 1
	wait(1)
end

while true do
	if Status.Value == 0 then
		print("Timer reached 0!")
		InRound.Value = true
		Players.PlayerAdded:Connect(function(player)
			MainEvent:FireClient(player)
		end)
		break
	end
	wait()
end
Client Script
local ReplicatedStorage = game.ReplicatedStorage

local CountdownEvent = ReplicatedStorage:WaitForChild("CountdownRemoteEvent")

local MainEvent = ReplicatedStorage.MainEvent

local Status = ReplicatedStorage:WaitForChild("Status")

local InRound = ReplicatedStorage:WaitForChild("InRound")

local Players = game:GetService("Players")

MainEvent.OnClientEvent:Connect(function()

print("Remote fired!")
end)

Thanks! :smiley:

What do you need to do on the client after you fire the event?

It doesn’t work because it fires before it can connect, try placing a wait() before the call:

wait()
MainEvent:FireClient(player)

Or put it in ReplicatedFirst and in the first few lines

1 Like

While true do loops without yeilds aren’t reccomended, you should use Value.Changed, it’s more reliable

I need It to manage the entire game then It will refresh the countdown soon after the round finishes.

It says Players.PlayerAdded:Connect… which doesn’t seem right. Are you sure you want to fire the event when a new player joins and the timer is 0?

1 Like

They don’t work. That’s why I use while true do. Plus there’s a break once the countdown reaches 0.

1 Like

Your player is added to the game before the Players.PlayerAdded function is called

Also what you did now will create a masive memory leak since it will make a new conection every loop

Also you probbely want to get the player currently in game so
game.Players:GetChildren() or game.Players:GetPlayers()

You don’t need to fire every time the countdown changes. What you could do is have a StringValue inside ReplicatedStorage and have the countdown change there (like what you did)

-- Server

for t = Status.Value, 1, -1 do
	Status.Value -= 1
	wait(1)
end

then have the GUI on the client change the text every time it detects a change in value (changes made from the server will always replicate to the client).

-- Client

local Text = script.Parent.TextLabel
local Status = game.ReplicatedStorage:WaitForChild("Status")

Status.Changed:Connect(function()
    Text.Text = Status.Value
end)

Now, through this, you don’t need to worry about if a new player will be able to see the active countdown, they will.

I think that’s not how you want to implement the code, since it keeps adding up new connections for every time Status.Value == 0.

Does this work instead?

-- Ridiculous janky method that works
while true do
	for t = Status.Value, 1, -1 do
		CountdownEvent:FireAllClients(t)
		Status.Value -= 1
		wait(1)
	end
	
	MainEvent:FireAllClients()
end

Im guessing your trying to fire the remote to all the players currently inside, if so maybe try this.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local CountdownEvent = ReplicatedStorage:WaitForChild("CountdownRemoteEvent")
local MainEvent = ReplicatedStorage:WaitForChild("MainEvent")
local Status = ReplicatedStorage:WaitForChild("Status")
local InRound = ReplicatedStorage:WaitForChild("InRound")

for t = Status.Value, 1, -1 do
	CountdownEvent:FireAllClients(t)
	Status.Value -= 1
	wait(1)
end

InRound.Value = true
MainEvent:FireAllClients()