Hello! I’m trying to create raycasting to damage other players in my game, but it’s not working correctly. I can deal damage to other players, but something is off because I need to get very close in FPS mode to damage another player, while it works normally in TPS. What could be wrong?

This is my function to perform raycasting:
-- Get references to the local player and mouse
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
-- Get a reference to the server-side event
local fireEvent = script.Parent.gunFire
local playerGui = player:WaitForChild("PlayerGui")
-- Cross mouse icons
local mouseIconFPS = playerGui:WaitForChild("FPS_Mouse_Cross")
local mouseIconTPS = game:GetService("UserInputService")
-- Function to perform raycasting and return the target humanoid
-- Assuming your tool has a part named "Handle" that you want to use as the origin of the ray
local handle = script.Parent:FindFirstChild("Handle")
local function performRaycast()
if not handle or not handle:IsA("BasePart") then
-- Make sure the "Handle" part exists and is a BasePart
return nil, Vector3.new()
end
local ray = Ray.new(handle.Position, (mouse.Hit.p - handle.Position).unit * 300)
local hitPart, hitPosition = workspace:FindPartOnRay(ray, player.Character, false, true)
if hitPart then
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
return humanoid, hitPosition
end
end
return nil, hitPosition
end
-- Connect the script to the Equipped event
script.Parent.Equipped:Connect(function()
-- Check if the camera mode is LockFirstPerson
if player.CameraMode == Enum.CameraMode.LockFirstPerson then
task.wait(0.2)
mouseIconFPS.Enabled = true
mouseIconTPS.MouseIconEnabled = false
mouseIconTPS.MouseIcon = ""
else
mouseIconFPS.Enabled = false
mouseIconTPS.MouseIconEnabled = true
mouseIconTPS.MouseIcon = "rbxassetid://15615486050"
end
end)
-- Connect the script to the Unequipped event
script.Parent.Unequipped:Connect(function()
task.wait(0.2)
mouseIconFPS.Enabled = false
mouseIconTPS.MouseIconEnabled = false
mouseIconTPS.MouseIcon = ""
end)
-- Connect the script to the Activated event
script.Parent.Activated:Connect(function()
-- Check if the tool is equipped
if script.Parent:IsA("Tool") and script.Parent.Parent and script.Parent.Parent:IsA("Model") then
-- Check if the camera mode is LockFirstPerson
if player.CameraMode == Enum.CameraMode.LockFirstPerson then
mouseIconFPS.Enabled = true
mouseIconTPS.MouseIconEnabled = false
mouseIconTPS.MouseIcon = ""
-- Perform raycast to determine the target
local targetPart, targetPosition = performRaycast()
if targetPart and targetPart.Parent then
-- Fire the server event with the parent of the target part
fireEvent:FireServer(targetPart.Parent, targetPosition)
end
else
mouseIconFPS.Enabled = false
mouseIconTPS.MouseIconEnabled = true
mouseIconTPS.MouseIcon = "rbxassetid://15615486050"
-- Perform raycast to determine the target
local targetPart, targetPosition = performRaycast()
if targetPart and targetPart.Parent then
-- Fire the server event with the parent of the target part
fireEvent:FireServer(targetPart.Parent, targetPosition)
end
end
else
mouseIconFPS.Enabled = false
mouseIconTPS.MouseIconEnabled = false
mouseIconTPS.MouseIcon = ""
end
end)
this is my server-side script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gunFireEvent = script.Parent:WaitForChild("gunFire")
gunFireEvent.OnServerEvent:Connect(function(player, target, hitPosition)
-- Validate that the target has a humanoid
local humanoid = target:FindFirstChildOfClass("Humanoid")
if humanoid then
-- Calculate the damage
local damage = 15
-- Apply the damage to the humanoid
humanoid:TakeDamage(damage)
-- Print a message to the server console for debugging
print(player.Name .. " damaged " .. target.Name .. " for " .. damage .. " health.")
end
end)