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
Never had any issues with this type of thing in the past, just having it now.
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.
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.
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?
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)
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.
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?
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).
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
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?
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.