For some reason, my raycast is either not going towards the player or going in a completely different direction and I have absolutely no clue on why it is happening. Any help?
for i,v in pairs (Players_In_Range) do
local Distance = (Throwable_Clone.Position - v.Character.HumanoidRootPart.Position).Magnitude
if Distance <= BlastRadius then
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {Throwable_Clone}
local raycast = workspace:Raycast(Throwable_Clone.Position,v.Character.HumanoidRootPart.Position,params)
print(raycast)
if raycast.Instance:FindFirstChild("Humanoid") then
if raycast.Instance:FindFirstChild("Humanoid").Health <= 0 then
else
raycast.Instance:FindFirstChild("Humanoid").Health = raycast.Instance:FindFirstChild("Humanoid").Health - (Damage - (Damage * (Distance/BlastRadius)))
end
end
end
end
Pretty much it loops through a table of players and takes the nade’s position the shoots a ray at the said player. It is accurately grabbing both the nade and the player’s position since I’ve already checked those two with print statements. I’m just confused on why the ray isn’t casting correctly.
Maybe you could check if there’s a valid raycast first? If there is, you could print the results
if raycast then
print("Object/terrain hit:", raycastResult.Instance:GetFullName())
print("Hit position:", raycastResult.Position)
print("Surface normal at the point of intersection:", raycastResult.Normal)
print("Material hit:", raycastResult.Material.Name)
if raycast.Instance:FindFirstChild("Humanoid") and raycast.Instance:FindFirstChild("Humanoid").Health <= 0 then
else
raycast.Instance:FindFirstChild("Humanoid") and raycast.Instance:FindFirstChild("Humanoid").Health = raycast.Instance:FindFirstChild("Humanoid").Health - (Damage - (Damage * (Distance/BlastRadius)))
end
else
print("No result found")
end
local BlastRadius = 40
local Damage = 150
local Throwable_Clone = workspace.grrr
local Players_In_Range = {}
local function Find_Players(nade)
table.clear(Players_In_Range)
for i,v in pairs (game.Players:GetChildren())do
if v.Character then
if v.Character:WaitForChild("HumanoidRootPart") then
if v.Character:WaitForChild("Humanoid").Health > 0 then
if (v.Character.HumanoidRootPart.Position - nade.Position).Magnitude <= BlastRadius then
local Distance = (v.Character.HumanoidRootPart.Position - nade.Position).Magnitude
table.insert(Players_In_Range,{plr=v,dis=Distance})
print(v.Name)
return Players_In_Range
end
end
end
end
end
end
Find_Players(Throwable_Clone)
wait()
local function ONLYPlayers()
local t = {Throwable_Clone}
for i ,v in pairs(game.Players:GetPlayers()) do
if v.Character then
for i, ob in pairs(v.Character:GetChildren()) do
if ob:IsA("Accessory") or ob:IsA("Hat") then
table.insert(t,ob)
end
end
end
end
for i ,v in pairs(workspace:GetDescendants()) do
if not v:IsA("BasePart") then continue end
if v.Transparency==1 then
table.insert(t,v)
end
end
return t
end
for i,v in pairs (Players_In_Range) do
local plr = v.plr
local dis = v.dis
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = ONLYPlayers()
local raycast = workspace:Raycast(Throwable_Clone.Position,(plr.Character.HumanoidRootPart.CFrame.p-Throwable_Clone.CFrame.p).Unit*9000,params)
if raycast.Instance.Parent:FindFirstChild("Humanoid") then
if raycast.Instance.Parent:FindFirstChild("Humanoid").Health > 0 then
print( Damage - (Damage * (dis/BlastRadius)) )
raycast.Instance.Parent:FindFirstChild("Humanoid").Health -=(Damage - (Damage * (dis/BlastRadius)))
end
end
end