My gun raycast script dosen't detect players most of the time

So my gun script gives me no errors but when i shoot at someone it dosen’t damage it most of the time
i’ve tried alot, at first i thought that it was hitting the player even though i ignored it but that wasn’t it
so i don’t know what to do, please help

raydirection = mouse.Hit.p - cam.CFrame.Position
result = workspace:Raycast(cam.CFrame.Position, raydirection, newray)
if result then
print(result.Instance.Name)
game.ReplicatedStorage.ShootGun:FireServer(result, vm.Damage.Value)
end

robloxapp-20230824-1417044.wmv (2.0 MB)

Can you show how the newray RaycastParams is constructed?

local newray = RaycastParams.new()
newray.FilterDescendantsInstances = {char}
newray.FilterType = Enum.RaycastFilterType.Exclude

This may be happening because you’re sending the raycast to the Mouse’s target position, which means that it may not intercept the part you’re aiming at. You can likely resolve this by passing Mouse.Target (returns the part that the mouse is targeting) into the RemoteEvent if result is nil.

Thank you so much! that seemed to work! Now I’m only using mouse.Target and checking if it’s pointing at a player

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.