I’m trying to make a gun using raycasts. If the object the raycast is hitting is a descendant of the Map folder, then the ray shouldn’t go through it. The problem with it is that it doesn’t work. Anyone able to help?
Code:
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
local descendants = game.Workspace.Map:GetDescendants()
for index, descendant in pairs(descendants) do
if hitPart == descendant then
print("Object in the way!")
else
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid:TakeDamage(7)
end
end
end
end
end
end)
Ok, instead of trying to see if it’s a descendant trought a loop, you can just use :IsDescendantOf(), it is a function that tells if the object is descendant of the variable inside it, if it is, it returns true, if its not, it returns false.
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart:IsDescendantOf(workspace.Map) then -- this function verifies if it is a descendant of Map for you
print("Object in the way!")
else
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then -- if it has a humanoid
humanoid:TakeDamage(7)
end
end
end
end)
Actually that shouldn’t happen, as you are only ignoring the character, unless the wall was inside of it.
Maybe you can try to use this raycast instead to see if something changes:
local hitPart = workspace:FindPartOnRay(Ray.new(script.Parent.Handle.Muzzle.Position,mousePos - script.Parent.Handle.Muzzle.Position),player.Character)
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Muzzle.Position, (mousePos - script.Parent.Handle.Muzzle.Position).Unit * 300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart:IsDescendantOf(workspace.Map) then -- this function verifies if it is a descendant of Map for you
print("Object in the way!")
else
local humanoid = hitPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then -- if it has a humanoid
humanoid:TakeDamage(7)
end
end
else
print("Nothing hit apparently?")
end
end)