I want to make a gun, but I have to use raycasting to do that. (I want to make my own because I want to make an AI for it.)
Most of the time raycast returns nil, sometimes it raycasts to random objects that I didn’t expect for it to raycast to.
I found a way to show the raycasts, but it is still as confusing.
Server
script.Parent.Equipped:Connect(function()
script.Parent.Mesh.CanCollide = false
end)
local flare = script.Parent.Flare
script.Parent.Shoot.OnServerEvent:Connect(function(plr, target)
local raycpr = RaycastParams.new()
raycpr.FilterType = Enum.RaycastFilterType.Blacklist
raycpr.FilterDescendantsInstances = {script.Parent}
local rayc = game.Workspace:Raycast(flare.Position, (flare.Position - target.Position).Unit, raycpr)
if rayc == nil then
print("Raycast is nil! Must have done something wrong..")
else
print(rayc.Instance:GetFullName())
local distance = (flare.Position - rayc.Position).Magnitude
local p = Instance.new("Part")
p.Parent = game.Workspace
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(flare.Position, rayc.Position)*CFrame.new(0, 0, -distance/2)
end
end)
Local
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
script.Parent.Activated:Connect(function()
local target = mouse.Target
script.Parent.Shoot:FireServer(target)
end)