Hello everyone! So i’m trying to make a sphere that uses raycasting to damage the player if it detects a humanoid.
If you look in the output it keeps printing “Handle” and it’s probably because my avatar had a banana suit and i don’t know how to locate it in FilterDescendantsInstances
Here’s the script:
while wait(1) do
local LaserSphere = script.Parent
local Position = LaserSphere.Position
local Direction = -LaserSphere.CFrame.RightVector
local rcParams = RaycastParams.new()
rcParams.FilterDescendantsInstances = {LaserSphere}
rcParams.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(Position, Direction * 50)
if ray then
local Part = ray.Instance
print(Part)
if Part.Parent:FindFirstChild("Humanoid") then
Part.Parent.Humanoid.Health -= 10
end
end
end
I believe you are trying to make a laser damage a player when it crosses the ray from a sphere. Why would you not want the Handle to be involved? The handle is probably an accessory you have equipped on your character
When a part is hit, check its parent, then check that parent’s parent, and keep going until either, the part has a humanoid as a child, or you reach workspace.
local Part = ray.Instance
local char = Part:FindFirstAncestorWhichIsA("Model")
if char ~= nil then
local h = char:findFirstChild("Humanoid")
if h ~= nil then
h:TakeDamage(10)
end
end
function checkPart (part)
if part:FindFirstChild("Humanoid") then
return true
end
if part.Parent == nil or part.Parent == workspace then
return false
end
return checkPart(part.Parent)
end
while wait(1) do
local LaserSphere = script.Parent
local Position = LaserSphere.Position
local Direction = -LaserSphere.CFrame.RightVector
local rcParams = RaycastParams.new()
rcParams.FilterDescendantsInstances = {LaserSphere}
rcParams.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(Position, Direction * 50)
if ray then
local Part = ray.Instance
print(Part)
local char = Part:FindFirstAncestorWhichIsA("Model")
if char ~= nil then
local h = char:findFirstChild("Humanoid")
if h ~= nil then
h:TakeDamage(10)
end
end
end
end
Also if this does solve your issue, please mark it as solved
You can set the CanQuery property of the accessory handles to false when a character spawns in, which prevents them from being included in operations like raycasting.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
task.wait(0.5) -- small wait otherwise this might not work properly
for _, v in pairs(character:GetDescendants()) do
if v.Name == "Handle" and v.Parent:IsA("Accessory") then
v.CanQuery = false
end
end
end
end