Client not getting RemoteEvent:FireClient, whats the issue?

Hello, I was scripting an system that server script will fire every players an event one at a time.

The problem is that Client is not getting any events when server fires it.
I tried to fix this solution by looking at API resources and other posts but I couldn’t find the solution for it.
Could you help me find the issue with my script?

-Server

for i, player in pairs(game.Players:GetChildren()) do
	local index = table.find(playerList, player)
				
	for i, player in pairs(game.Players:GetChildren()) do
		player:LoadCharacter()
		player.Character:FindFirstChild("HumanoidRootPart").CFrame = game.Workspace.Building.CFrame
	end
				
	if index then
		print (player)
		ReplicatedStorage.PlayerChoosen:FireClient(player)
		Status.Value = "Showing "..(player.Name).."'s design"
		wait(10)
					
		for i, structures in pairs(game.Workspace:GetChildren()) do
			if structures.ClassName == ("Model") then
				if structures:FindFirstChild("Primary") then
					structures:Destroy()
				end
			end
		end
					
	end
end
		

-Local

local function OnPlayerChoosen()
	print ("Choosen")
	for i,v in pairs (playerPlacedStructuresDictionary) do
		game.ReplicatedStorage.Remote.PlaceStructure:InvokeServer(playerPlacedStructuresDictionary[i][1],playerPlacedStructuresDictionary[i][2])
	end
end

PlayerChoosen.OnClientEvent:Connect(OnPlayerChoosen)		

The client is not getting the event, print (“Choosen”) is not printing out

Do you get any errors or does it just not print “choosen”?

I don’t get any errors and it does not print “choosen”

Does the code ever reach the FireClient? If it does, could there be something that’s preventing the OnClientEvent from being created?

Yes, status.Value changes when the loop runs but not the event for somereason.

Is there more code in this localscript? If so, may we see?

Whole thing is 300 lines, I don’t think there is problem with it but I will post it in a sec

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage.Remote:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")
local ReadyToPlaceStructure = ReplicatedStorage.Remote:WaitForChild("ReadyToPlaceStructure")
local HiddenStatus = game.ReplicatedStorage.HiddenStatus
local PlayerChoosen = ReplicatedStorage.PlayerChoosen

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 2
local maxPlacingDistance = 50
local rKeyIsPressed = false
local placingStructure = false
local yOrientation= 0
local goodTOPlace = false
local clientStructure
local gridSize = 2

local currentBlock = nil
local animCFrame = nil
local gridLock = true

local hit
local beingReplaced = false
local replacedStructureName = nil
local replacedStructureCFrame = nil

local rotateLock = true


local budget = player.Budget
local BudgetSign = script.Parent.BudgetSign

local isOnGui = false
local InfoGui = player.PlayerGui.InfoGui

BudgetSign.Text = ("$")..budget.Value

local playerPlacedStructuresDictionary = {
	
}

local structureNumber = 0


-- Handles start of temporary brick rotation
function handleKeyInputStarted(input)
	rKeyIsPressed = true

	local rotationSpeed = 5
	while rKeyIsPressed do
		wait()
		if placingStructure == true then
			if rotateLock == true then
				yOrientation = yOrientation - (yOrientation%45)
				yOrientation = yOrientation + 45
				ReplicatedStorage.Sfx.Rotate:Play()
				rKeyIsPressed = false
			else
				rKeyIsPressed = true

				local rotationSpeed = 5
				while rKeyIsPressed do
					wait()
					if placingStructure == true then
						yOrientation = yOrientation + rotationSpeed	
						ReplicatedStorage.Sfx.Rotate:Play()
					end
				end
				
			end
		end
	end

end


-- Handles end of temporary brick rotation
function handleKeyInputEnded(input)
	if input.KeyCode == Enum.KeyCode.R then
		rKeyIsPressed = false						
	end
end


-- Handles constructing the temporary placement brick when the create button is pressed. OOOOOOOOOOOOOOOOOOO
local function onReadyToPlaceStructure(structureName)

	if placingStructure == false then
		placingStructure = true
		clientStructure = Structures:FindFirstChild(structureName):Clone()
		clientStructure.Parent = game.Workspace
		RunService.RenderStepped:Connect(handleRenderStepped)

		char:FindFirstChildOfClass("Humanoid").Died:Connect(function()
			if clientStructure then
				clientStructure:Destroy()
				placingStructure = false
			end
		end)
	end
end

function ShowInfoGui()
	if placingStructure == true then
		InfoGui.Enabled = true
		InfoGui.Frame.Position = UDim2.new(0, mouse.x + 0.2, 0, mouse.y + 0.1)
	else
		InfoGui.Enabled = false
	end
end

ReplicatedStorage.HiddenStatus:GetPropertyChangedSignal("Value"):Connect(function()
	if ReplicatedStorage.HiddenStatus.Value == "Vote" then
		placingStructure = false
		
		if clientStructure then
			clientStructure:Destroy()
		end
	end
end)

-- Handles construction of actual Brick when user is satisfied with placement/rotation 
function handleMouseInputBegan(input)
	RunService.RenderStepped:Connect(ShowInfoGui)
	if placingStructure ==  true then
		if goodTOPlace == true then
			local StructureCFrame = clientStructure.PrimaryPart.CFrame			
			
			placingLocalStructure(clientStructure.Name, StructureCFrame)

		else

			game.ReplicatedStorage.Remote.ValueChange:InvokeServer(clientStructure.Name, 1, nil, nil)
			ReplicatedStorage.Sfx.Error:Play()
			placingStructure = false
			clientStructure:Destroy()
		end
	else if mouse.Target then
			hit = mouse.Target

			if hit.Parent:FindFirstChild("Primary") and isOnGui == false then
				replacedStructureName = mouse.Target.Parent.Name
				replacedStructureCFrame = mouse.Target.Parent.PrimaryPart.CFrame
				onReadyToPlaceStructure(mouse.Target.Parent.Name)
				ReplicatedStorage.Remote.DeleteStructure:InvokeServer(hit.Parent)
				hit.Parent:Destroy()
				beingReplaced = true
			end
		end
	end
end

function placingLocalStructure(clientStructureName, StructureCFrame)
	local realStructure = Structures:FindFirstChild(clientStructureName):Clone()
	
	if realStructure then
		realStructure:SetPrimaryPartCFrame(StructureCFrame)
		realStructure.Parent = game.Workspace
		ReplicatedStorage.Sfx.Placement:Play()
		
		structureNumber = structureNumber + 1
		playerPlacedStructuresDictionary[structureNumber] = {clientStructureName, StructureCFrame}
		clientStructure:Destroy()

	else
		clientStructure:Destroy()
	end
	
	placingStructure = false
end

function quitPlacingStructure()
	if beingReplaced == true then
		
		clientStructure:Destroy()
		
		
		placingStructure = false
		ReplicatedStorage.Sfx.Quit:Play()

		placingLocalStructure(replacedStructureName,replacedStructureCFrame)
		
		placingStructure = false
		replacedStructureName = nil
		replacedStructureCFrame = nil
		beingReplaced = false
		
	else

		game.ReplicatedStorage.Remote.ValueChange:InvokeServer(clientStructure.Name, 1, nil, nil)
		clientStructure:Destroy()
		
		placingStructure = false
		ReplicatedStorage.Sfx.Quit:Play()
		
		
		replacedStructureName = nil
		replacedStructureCFrame = nil
		beingReplaced = false
	end
	
end

script.Parent.RotateLock.MouseButton1Up:Connect(function()
	if rotateLock == true then
		rotateLock = false
		script.Parent.RotateLock.Text = ("RotateLock = FALSE")
		ReplicatedStorage.Sfx.Click:Play()
	else
		rotateLock = true
		script.Parent.RotateLock.Text = ("RotateLock = TRUE")
		ReplicatedStorage.Sfx.Click:Play()
	end
end)

-- Handles general input (mouse or keyboard) input
function handleInputStarted(input)
	if input.KeyCode == Enum.KeyCode.R then
		handleKeyInputStarted(input)
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		handleMouseInputBegan(input)
	end
	
	if input.KeyCode == Enum.KeyCode.G then
		if gridLock == false then
			gridLock = true
			script.Parent.InfoButton.Grid.Text = "GridLock(G) : ON"
			ReplicatedStorage.Sfx.Click:Play()
		elseif gridLock == true then
			gridLock = false
			script.Parent.InfoButton.Grid.Text = "GridLock(G) : OFF"
			ReplicatedStorage.Sfx.Click:Play()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.T then
		if placingStructure == true then
			placingStructure = false
			ReplicatedStorage.Remote.DeleteStructure:InvokeServer(clientStructure)
			local sellprice = clientStructure:GetAttribute("Price")
			ReplicatedStorage.Remote.BudgetValueChange:InvokeServer(sellprice)
			clientStructure:Destroy()
			ReplicatedStorage.Sfx.Sell:Play()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Y then
		if placingStructure == true then
			game.ReplicatedStorage.Remote.ValueChange:InvokeServer(clientStructure.Name, 1, nil, nil)
			clientStructure:Destroy()

			placingStructure = false
			ReplicatedStorage.Sfx.Quit:Play()


			replacedStructureName = nil
			replacedStructureCFrame = nil
			beingReplaced = false
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Q then
		if placingStructure == true then
			quitPlacingStructure()
		end
	end
end

-- Handles rendering the temporary Brick during placement
function handleRenderStepped()
	if placingStructure == true then
		local mouseRay = mouse.UnitRay
		local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
		local ignoreList = {clientStructure, char}
		local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)

		local Children = clientStructure:GetChildren()
		
		local price = clientStructure:GetAttribute("Price")
		
		if hit and hit.Name == "Floor" and (HumanoidRootPart.Position - clientStructure.PrimaryPart.Position).Magnitude < maxPlacingDistance then
			goodTOPlace = true

			for i = 1, #Children do
				if Children[i].ClassName == "Part" then
					Children[i].Color = Color3.fromRGB(85, 255, 127)
					Children[i].CanCollide = false
				end
			end
		else
			goodTOPlace = false

			for i = 1, #Children do
				if Children[i].ClassName == "Part" then
					Children[i].Color = Color3.fromRGB(255, 0, 0)
					Children[i].CanCollide = false
				end
			end
		end



		local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)

		if gridLock == true then
			local newCFrame = CFrame.new(math.floor(position.X / gridSize) * gridSize, position.Y + clientStructure.PrimaryPart.Size.Y/2, math.floor(position.Z / gridSize) * gridSize)
			clientStructure:SetPrimaryPartCFrame(newCFrame*newAnglesCFrame)

		else
			local newCFrame = CFrame.new(position.X, position.Y + clientStructure.PrimaryPart.Size.Y/2, position.z)
			clientStructure:SetPrimaryPartCFrame(newCFrame*newAnglesCFrame)
		end
	end
end

function handleGridButtonPressed(gridName)
	gridSize = gridName
	ReplicatedStorage.Sfx.Click:Play()
end
-- Iterate over all Buttons in the StructureFrame and attach a mouse listener



for i,v in pairs(script.Parent.Parent:GetDescendants()) do
	if v.ClassName == "TextLabel" or v.ClassName == "TextButton" or v.ClassName == "ScrollingFrame" or v.ClassName == "Frame" then
		v.MouseEnter:Connect(function()
			isOnGui = true
		end)
		v.MouseLeave:Connect(function()
			isOnGui = false
		end)
	end
end

HiddenStatus:GetPropertyChangedSignal("Value"):Connect(function()
	if HiddenStatus.Value == "Vote" then
		for i, structures in pairs(game.Workspace:GetChildren()) do
			if structures.ClassName == ("Model") then
				if structures:FindFirstChild("Primary") then
					structures:Destroy()
				end
			end
		end
	end
end)

local function OnPlayerChoosen()
	print ("Choosen")
	for i,v in pairs (playerPlacedStructuresDictionary) do
		game.ReplicatedStorage.Remote.PlaceStructure:InvokeServer(playerPlacedStructuresDictionary[i][1],playerPlacedStructuresDictionary[i][2])
	end
end

PlayerChoosen.OnClientEvent:Connect(OnPlayerChoosen)
ReadyToPlaceStructure.OnClientInvoke = onReadyToPlaceStructure



UIS.InputBegan:Connect(handleInputStarted)
UIS.InputEnded:Connect(handleKeyInputEnded)
1 Like

The thing is that this code actually functioned correctly yesterday, I didn’t touched a single code in a script from what I remember, the error just came today

There doesn’t seem to be anything directly causing them, try putting the OnClientEvent as the very first thing connected?

That or it mayyyy be that it’s firing the ClientEvent before it is made?

1 Like

Hmm yea it could be because of it. One other thing I want to ask is is there alternative way for RemoteEvent:FireClient and Client receiving the event?

The only alternative is FireAllClients, which as the name says, fires to every client, which you probably don’t want to use.

Perhaps give some time for the client to create their OnClientEvent before Firing to it?

1 Like

Ok how would I give the tinme for the client to create onClientEvent?

If I may ask, when is the server code ran?

about 40 second after game started

So it can’t be that it has no time for the fireclient to be made, maybe try what I had mentioned before and put the OnClientEvent code above all functions?

I tried that but It got the same result before :frowning:

Where is the local script placed???

Its on the Startergui.gui for other things in local script

I wonder if this is just because of my game and it would give different result when I try it on new game.