Alright, so for my game i’m working on making my guns mobile compatible. I made a custom UI to go with it.
The raycasting in my guns works fine when using mouse.Hit.Position, for example:
gun.Fire:FireServer(mouse.Hit.Position)
this will work fine, and my script will fire the bullet to the right mouse position.
For my mobile UI however, i just take the middle of the screen instead of a mouse Position and the script returns an error.
local Vector = Vector2.new(workspace.CurrentCamera.ViewportSize.X / 2, workspace.CurrentCamera.ViewportSize.Y / 2 - (game:GetService("GuiService"):GetGuiInset().Y/2))
gun.Fire:FireServer(Vector)
The above code returns this error:

(I looked up how to get the middle of the screen on another devforum post, so I’m not sure if the calculation is accurate.)
The thing is, when i print the “MousePos” (or in this case the “Vector” variable from the code snippet) it prints out 2 numbers: 1 X, 1 Y and of course, no Z value, because it is a Vector2, not a Vector3.
Any tips?
Server side looks like this:
gun.Fire.OnServerEvent:Connect(function(player, mousePos)
print(mousePos)
local char = gun.Parent
local humanoidChar = char:WaitForChild("Humanoid")
if humanoidChar.Health ~= 0 then
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {player.Character, game.Workspace.SniperZoneModel}
local handlepos = mainmodel.Position
local raycastResult = workspace:Raycast(handlepos, (mousePos - handlepos)*300, raycastParams)
if raycastResult then
if AmmoVal.Value ~= 0 then
firesound:Play()
FX:Emit(1)
enableLight()
AmmoVal.Value = AmmoVal.Value - 1
local SPREAD = 0.4
local finalPos = raycastResult.Position + Vector3.new(math.random(-SPREAD, SPREAD), math.random(-SPREAD ,SPREAD), math.random(-SPREAD, SPREAD))
if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
raycastResult.Instance.Parent.Humanoid.Health = raycastResult.Instance.Parent.Humanoid.Health - Damage
rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, true)
HitsModule.AddHits(raycastResult.Instance.Parent, char.Name, Damage)
EnemyHitEvent:FireClient(player)
elseif raycastResult.Instance.Parent.Parent:FindFirstChild("Humanoid") then
raycastResult.Instance.Parent.Parent.Humanoid.Health = raycastResult.Instance.Parent.Parent.Humanoid.Health - Damage
rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, true)
HitsModule.AddHits(raycastResult.Instance.Parent.Parent, char.Name, Damage)
else
rs.VisualizeBullet:FireAllClients(att.WorldPosition, finalPos, false)
end
else
reloadsound:Play()
task.wait(3)
AmmoVal.Value = ClipSizeVal.Value
end
end
end
end)
Thank you.
Edit: the error is at line 58, which is this line:
local raycastResult = workspace:Raycast(handlepos, (mousePos - handlepos)*300, raycastParams)