i made a simple raycast but sometimes when I click on objects the ray gous right through them.
code:
local plr = game.Players.LocalPlayer
local tool = script.Parent
local block = tool.gun
local mouse = plr:GetMouse()
local event = game.ReplicatedStorage.shoot
function inviswall(x, a, b) --x = hierarchy to search; a = transparency; b = cancollide
for i,v in pairs(x:GetChildren()) do
if v:IsA("Part") then
if v.Transparency == a and v.CanCollide == b then
return v
end
end
end
end
function getkids(a)
for i,v in pairs(a:GetChildren()) do
return v
end
end
local gun = getkids(tool)
local inviswalls = inviswall(workspace, 1, true)
print(inviswalls)
local function shoot()
local direction = (mouse.hit.Position - block.Position) * 9000
local rayparams = RaycastParams.new()
rayparams.FilterDescendantsInstances = {inviswalls, gun}
rayparams.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(block.Position, direction, rayparams)
print(ray)
end
tool.Activated:Connect(function()
shoot()
end)