What do you want to achieve?
I was making a gun, then this error popped up.
What is the issue?
In the output, on line 58 keeps printing this error "
Players.MalAllie.Backpack.Sniper Rifle.Server:58: attempt to perform arithmetic (sub) on nil and Vector3
"
What solutions have you tried so far?
I’ve tried printing to see which argument is nil. The mousePos variable is nil. I can’t figure out why??? On the client, it seems perfectly fine??? I used similiar code like this but this one errors???
--Server
RemoteShoot.OnServerEvent:Connect(function(plr, mousePos: Vector3)
if debounce or not plr.Character then
return
end
if Ammo.Value <= 0 then
reload(plr)
return
end
local character = plr.Character
local head: BasePart = character:WaitForChild("Head")
local myHRP: Part = character:WaitForChild("HumanoidRootPart")
local origin = head.Position
params:AddToFilter(getAllCharactersInTeam(plr.Team))
local cast = workspace:Raycast(origin, (mousePos - origin), params) --this is line 58!!
if cast then
local model: Model = cast.Instance:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChildOfClass("Humanoid")
if humanoid then
local enemyPlr = game.Players:GetPlayerFromCharacter(model)
local isAnEnemy = false
if enemyPlr then
if enemyPlr.Team ~= plr.Team then
isAnEnemy = true
local eProp: Model = model:WaitForChild("Properties")
local playerDamagedMe: ObjectValue = eProp:WaitForChild("PlayerDamagedMe")
playerDamagedMe.Value = plr
end
end
if not enemyPlr then
isAnEnemy = true
end
if isAnEnemy then
local dmg = 45
if cast.Instance.Name == "Head" then
dmg = 45*3
end
humanoid:TakeDamage(dmg)
KBConstructor.new(model:WaitForChild("HumanoidRootPart"), myHRP.CFrame.LookVector.Unit*dmg/3, 25)
DamageDisplay:FireClient(plr, cast.Position, dmg ~= 6 and true or false, dmg)
end
end
end
end
debounce = true
task.wait(.1)
debounce = false
end)
--Client
Tool.Activated:Connect(function()
local mousePos = mouse.Hit.p
if mousePos == nil then
print("mousePos == nil") return
end
RemoteShoot:FireServer(mousePos)
end)
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Tool = script.Parent
local Sniper = Tool:WaitForChild("Sniper")
local RemoteShoot = Tool:WaitForChild("RemoteShoot")
local plr = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = plr:GetMouse()
local GuiScope: ScreenGui = plr.PlayerGui:WaitForChild("Scope")
GuiScope.Enabled = false
local equipped = false
local isScoped = false
local currentLerp = nil
Sniper.Transparency = 1
local function reload()
RemoteShoot:FireServer()
end
Tool.Activated:Connect(function()
local mousePos = mouse.Hit.p
if mousePos == nil then
print("mousePos == nil") return
end
RemoteShoot:FireServer(mousePos)
end)
Hmm, well if you look at the bottom of your output. You’ll notice it prints “nil from server” three times. That’s not the problem though, the problem is that it should print it in pattern as such:
"nil from client"
"nil from server"
"nil from client"
"nil from server"
"nil from client"
"nil from server"
and so on…
You can see the pattern more clearly above in the output. How first it prints the mousePos from the client, and below it, the server’s.
Try pressing Ctrl+Shift+F to look for any other scripts that may contain the “RemoteShoot” RemoteEvent.
Also, in the server script, you should probably already be checking whether or not the mousePos variable is a Vector3 type as such:
if (typeof(mousePos) ~= "Vector3") then
return
end