RemoteEvent returning nil values

I am just making a quick gun system, and I am trying to send the Origin and Direction of a UnitRay to the server.

I have the UnitRay, and everything works went moved to the client perfectly. But when trying to to the raycast on the server, I find out that Origin and Direction are somehow nil on the server, while on the client they are actual Vector3s.

Client (LocalScript)

if UnitRay then
	fireRemote:FireServer(UnitRay.Origin,UnitRay.Direction)
end

Server (Script, prints nil)

remotes.Fire.OnServerEvent:Connect(function(player,origin: Vector3,direction: Vector3)
	if canFire == true and IsOwner(player) == true then
		canFire = false
		print(origin)
		print(direction)
		Fire(origin,direction)
		canFire = true
	end
end)

both the prints are at lines 137 and 138 btw
image

Never had any issues with this type of thing in the past, just having it now.

3 Likes

Is what you are firing nli? Like the DIrection or the Origin, id try printing out the values on the client if you haven’t already, or just only fire the remote if those values arent’t nil.

2 Likes

It is likely that the values ​​you are sending by the client are equal to nil. check whether the values ​​are being correctly defined.

1 Like

I have already checked the values on the client though. They are both their respective Vector3s. I was even able to do the raycast on the client, but obviously can’t send the results from it since it still was nil.

image

1 Like

I think it’s nil because the raycast it’self… maybe try sending fireRemote:FireServer(Vector3.new(UnitRay.Origin.X,UnitRay.Origin.Y,UnitRay.Origin.Z),Vector3.new(UnitRay.Direction.X,UnitRay.Direction.Y,UnitRay.Direction.Z)) from the client?

1 Like

Still returns nil for some reason, even when making a completely different Vector3.

1 Like

can you send all code? from the two context-sides.

2 Likes

Client (UnitRay sent values):

function Fire()
	if gun:GetAttribute("Ammo") > 0 then
		canFire = false
		repeat
			if gun:GetAttribute("Ammo") > 0 then
				playSound:FireServer("Fire")
				remotes.Fire:FireServer()
				fire:Play()
				recoil:Fire(recoilAmount)
				local x,y = RandomPointsInsideCrosshair(workspace:GetAttribute("CrosshairSize"))
				local UnitRay = camera:ScreenPointToRay(
					(crosshair_ABS.X / 2) + x,
					(crosshair_ABS.Y / 2) + y - guiService:GetGuiInset().Y
				)
				if UnitRay then
					print(UnitRay.Origin)
					print(UnitRay.Direction)
					fireRemote:FireServer(UnitRay.Origin,UnitRay.Direction)
				end
				task.wait(fireDelay)
			else
				firing = false
			end
		until firing == false
		canFire = true
	end
end

Server (prints are where I find out it is nil)

remotes.Fire.OnServerEvent:Connect(function(player,origin: Vector3,direction: Vector3)
	if canFire == true and IsOwner(player) == true then
		canFire = false
		print(origin) -- line 137
		print(direction) -- line 138
		Fire(origin,direction) -- fails due to the nil values
		canFire = true
	end
end)
1 Like

may I ask why do you need the “origin: Vector3” instead of just “origin”? I’ve never seen it used. Or is it just for this post?

1 Like

It helps with autocomplete.

no : Vector3 would result in it being a variable that doesn’t have properties or functions

the : Vector3 allows you to see properties and functions of the Vector3 class on that variable

Same output as long as the value is a valid Vector3, just autocomplete for properties and functions

I really have no use for it here, I’m just used to doing that because I use it a lot.

2 Likes

Are remotes.fire and fireRemote the same thing?

2 Likes

Is this causing it? Are they different remoteevents? This is the only thing I can think of since it’s empty. Try putting something random inside of the ()? I have no idea.

3 Likes

None of it would even function if they weren’t the same…

image
image

image

1 Like

try removing remotes.Fire:FireServer() and test the code

1 Like

If that is in the same script as remotes.Fire:FireServer(), then fireRemote would be the exact same thing as saying remotes.Fire. Am I missing something?

1 Like

Do you know what I mean? It would be the same as saying, for example:

local Map = workspace:FindFirstChild(“Map”)
local Base = Map:FindFirstChild(“Base”)

and

Map.Base

warn(Map.Base == Base) = true

They would be the same instance. If in doubt, try printing out print(fireRemote == remotes.Fire) and if it’s true then that’s why it’s erroring (because of that line with an empty parentheses is firing the same event).

1 Like

Its not the same code, the remotes.Fire is on the server, the FindFirstChild one is on the client

remotes.Fire:FireServer()

You are firing the first time with empty arguments.

1 Like

In the server script, you’re listening for remotes.Fire’s OnServerEvent:

While in the client script, you’re firing fireRemote with the UnitRay’s origin and direction, not remotes.Fire:

And as @Khamilis and @OniiSamaUwU correctly pointed out, you are firing the remotes.Fire event in the LocalScript, but aren’t providing any arguments:

This is why the server is receiving nil, you seem to be firing a different RemoteEvent with the UnitRay’s values, and you’re firing the correct RemoteEvent without passing any values. I suggest organizing your RemoteEvents a bit better to avoid situations like this, or at least naming them in a more descriptive way :slight_smile::+1:


I also would like to mention, if the fireRemote is the same RemoteEvent as remotes.Fire, then to fix your problem, you’ll need to delete the line previously mentioned where you’re firing it without any arguments, and I also recommend sticking to using fireRemote exclusively to keep your code consistent since, after all, what’s the point of storing the RemoteEvent in a variable if you’re going to index it anyway?

3 Likes

Im just gonna be honest with this one, I never noticed the remotes.Fire:FireServer() since I was focused on the one where I checked if UnitRay wasn’t nil.

2 Likes