Hello! I have this raycast system for my gun, the raycast lines are correct, but sometimes it doesn't do the if ray then
remote.OnServerEvent:Connect(function(player, action, mousePos)
--code that works...
local character = player.Character
if not character then return end
local head = character:FindFirstChild("Head")
if not head then return end
local origin = head.Position
local direction = (mousePos - origin)
local distance = direction.Magnitude
if distance == 0 then return end
local rayDirection = direction.Unit * math.min(distance, 300)
if Debug then
local debugRay = Instance.new("Part")
debugRay.Name = "DebugRay"
debugRay.Anchored = true
debugRay.CanCollide = false
debugRay.CanTouch = false
debugRay.CanQuery = false
debugRay.Material = Enum.Material.Neon
debugRay.Color = Color3.new(1, 0, 0)
local rayLength = rayDirection.Magnitude
debugRay.Size = Vector3.new(0.1, 0.1, rayLength)
debugRay.CFrame = CFrame.new(origin, origin + rayDirection) * CFrame.new(0, 0, -rayLength/2)
debugRay.Parent = workspace
end
local character = player.Character
if not character then return end
local blacklist = {GunHandle, GunHandle.Slide, GunHandle.Shell, GunHandle.Muzzle}
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.CanQuery = false
end
end
for i, v in pairs (player.Character:GetChildren()) do
if v:IsA("Accessory") then
local hnd = v:FindFirstChild("Handle")
if hnd then
hnd.CanQuery = false
end
end
end
local params = RaycastParams.new()
params.FilterDescendantsInstances = blacklist
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true
local ray = workspace:Raycast(origin, rayDirection)
if ray then --here is the problem
print(ray.Instance)
print(ray.Position)
local part = Instance.new("Part")
part.Name = "ShootedPosPart"
part.Parent = workspace
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.Size = Vector3.new(0.2, 0.2, 0.2)
part.Color = Color3.new(0, 0, 0)
part.Position = ray.Position
game.Debris:AddItem(part, 5)
local sound = ConcreteImpactSound:Clone()
sound.Parent = part
sound:Play()
local effect = game:GetService("ReplicatedStorage"):FindFirstChild("VFX"):FindFirstChild("Bullet"):Clone()
effect.Parent = part
effect.Enabled = true
task.wait(0.15)
effect.Enabled = false
end
task.wait(cooldownTime)
cooldown = false
end)
** You are now Level 1! **

