I am currently sending raycasts to all players and filtering those in range of 8 studs. It’s all working great. Here comes another filter I’m unable to achieve - Filtering only players in front of me.
I tried ways other than Raycasting, however Raycasting seems reasonable in my project to avoid getting the players behind obstacles/walls.
Here’s a code block taken out of my script which is responsible for my raycasting mess.
for i,v in pairs(game.Players:GetPlayers()) do
local damage = 50
local originc = character.HumanoidRootPart.CFrame --ray start pos
local targetc = v.Character.HumanoidRootPart.CFrame --ray target
local startPosition = character.HumanoidRootPart.Position --ray start pos
local lookAt = v.Character.HumanoidRootPart.Position --towards where the ray looks
local distance = (startPosition - lookAt).magnitude --how far the ray goes
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local ray = workspace:Raycast(startPosition, (lookAt-startPosition).Unit * distance*1.4, raycastParams)
local raylength = (startPosition - lookAt).Magnitude
--if v.Team ~= player.Team then
if ray then
if ray.Instance.Parent:FindFirstChildOfClass("Humanoid") then
print("ray hit: ", ray.Instance.AssemblyRootPart.Parent.Name)
print("how far away: ", raylength, " studs")
if raylength < 8 then
v.Character.Humanoid:TakeDamage(damage)
end
elseif ray.Instance.Parent.Name == "DestructibleWall" then
print'hitting destructible wall'
else
print("not hitting")
end
end
--end
end
I would grately appreciate any help with this. I wasn’t able to find any useful information using the search engine