Remote Event Won't Fire

So I want to make a sensor that detects if a player touched it and fire a remote event into the client and then make a dialogue box appear, but I keep getting this one error:

image

I can’t fire a remote event from the server to the client without getting this error, I scoured throughout the devforum but could not find a way to reference the player generally.

Workspace:
image

Server Script:

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local sensor = script.Parent
local Player = game:GetService("Players")

sensor.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	
	if humanoidRootPart then
		remoteEvent:FireClient(hit)
	end
end)

Local Script:

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local player = game:GetService("Players").LocalPlayer
local roboDialogue = game.StarterGui.Dialogues.RobotDialogue

remoteEvent.OnClientEvent:Connect(function(player)
	roboDialogue.Visible = true
	print("All secure!")
end)
2 Likes

the fireclient first parameter needs to be a player object, not a character child
do this instead:

remoteEvent:FireClient(Player:FindFirstChild(character.Name))
2 Likes

OnClientEvent does not give you the player. It gives you what you passed through the remote.

remoteEvent.OnClientEvent:Connect(function(player)

The player is actually hit, what you told the client in the server script.

remoteEvent:FireClient(hit)

So, the local script would look like this:

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local player = game:GetService("Players").LocalPlayer
local roboDialogue = game.StarterGui.Dialogues.RobotDialogue

remoteEvent.OnClientEvent:Connect(function(hit)
	roboDialogue.Visible = true
	print("All secure!")
end)

If you are confused you can read the api-reference: RemoteEvent.OnClientEvent

Simply put, firing to the server gives the server the player as the first argument. While firing to the client gives you what arguments you told it.

2 Likes

But the thing is the error is in the server script, not in the local script.

2 Likes

Oh, I apologize. You need to say this:

remoteEvent:FireClient(player, hit)

Player is the player you want to fire it to.

1 Like

Here is the fixed code:

Server:

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local sensor = script.Parent
local Player = game:GetService("Players")

sensor.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if humanoidRootPart then
		remoteEvent:FireClient(player, hit)
	end
end)

Client:

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local player = game:GetService("Players").LocalPlayer
local roboDialogue = game.StarterGui.Dialogues.RobotDialogue

remoteEvent.OnClientEvent:Connect(function(hit)
	roboDialogue.Visible = true
	print("All secure!")
end)
2 Likes

for your server script you have to get the player from the character with
:GetPlayerFromCharacter(). Be sure to check if it isn’t nil

1 Like

Sometimes it prints the player but sometimes it also prints out nil, what do I do from there?
image

2 Likes

It’s because we aren’t checking what it hits.

Server

local remoteEvent = game.ReplicatedStorage.Room1SensorEvent
local sensor = script.Parent
local Player = game:GetService("Players")

sensor.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- Checks if they have a humanoid
	local character = hit.Parent
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if humanoidRootPart then
		remoteEvent:FireClient(player, hit)
	end
end
end)

The sensor could be hitting anything and it would fire.

This line below would return nil if it can not get the character back.

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
1 Like

nope its .OnClientEvent, the original poster used the wrong kind of parameters to fire

1 Like

Guys the issue is solved, I looked back in the workspace image that I had posted and I just realized that local scripts don’t work in the workspace.