Remote Event argument nil in Server Script

I have a gun located in the camera of the client, and i wanted to make it shoot, but when i fire a remote event from the client with arguments, the first one gets nil (FirePart)

Error comes from line 8 of Server Script

Error :

ServerScriptService.FireEvent:8: ServerScriptService.FireEvent attempt to index nil with 'Position'

Code :

  • Local Script :
function shoot()
    if not Shooting then
        Shooting = true
        
        WeaponViewmodel.Gun.FirePart.Fire:Play()
        
        game.ReplicatedStorage.Events.Fire:FireServer(WeaponViewmodel.Gun.FirePart, mouse.Hit.Position)
        
        wait(60/800)
        
        Shooting = false
        
    end
end
  • Server Script (error) :
Events.Fire.OnServerEvent:Connect(function(player, FirePart, MouseHitPosition)
    print(FirePart, MouseHitPosition)
        
        local Bullet = game.ReplicatedStorage.Bullet:Clone()
        
        coroutine.wrap(function()
            Bullet.Parent = workspace
            Bullet.CFrame *= CFrame.new(FirePart.Position, MouseHitPosition)
            
            local loop
            
            loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
                Bullet.CFrame *= CFrame.new(0, 0, -10 * (dt*60))
                if(Bullet.Position - FirePart.Position).magnitude >= 1000 then
                    Bullet:Destroy()
                    loop:Disconnect()
                end
            end)
        end)()

Thanks for help

Which code is line 8? I would like to know so that i can better pinpoint the issue

Have you referenced mouse in your local script?

line 8 :

Bullet.CFrame *= CFrame.new(FirePart.Position, MouseHitPosition)

Mouse is good, only the FirePart is nil in Server Script

Don’t pass the fire part in the remote event. Instead, just save it as a variable. Adding on, I don’t think parts can be replicated on to the server but I don’t think that’s 100% true.

Maybe the gun only exists on client?

The Gun is located on the Client’s Camera
Maybe then,
How could I get it ?

Use a variable and also, why do you have it on the client’s camera?

I always had guns in workspace

Because its a viewmodel (Gun + Arms, Rig…)

Its parented to workspace.CurrentCamera so clients camera not workspace

Maybe just position it onto the camera.

What does it print that print statement?

I got it by getting Current Part position before sending it to the client

 game.ReplicatedStorage.Events.Fire:FireServer(WeaponViewmodel.Gun.FirePart.Position, mouse.Hit.Position) -- Adding position so it is referenced from client

So that means you fixed it right? Glad you’ve fixed it.

1 Like