Bullet not showing when shooting at Skybox?

I’m creating a MP5 gun tool and it works so far, however, when firing into the Skybox, no bullet trace shows up. It shows up when I’m firing at a basepart or a part.

shotEvent.OnServerEvent:Connect(function(player, firearmPosition, firearmOrientation, mousePos, shot, firearm)
	
	if firearm:WaitForChild("MagazineAmmo").Value > 0 and firearm:WaitForChild("CanShoot").Value == true and firearm.Parent.Name == player.Name then
		if firearm.Name == "MP5" and firearm:WaitForChild("MP5_Shot") then
			-- Decrease magazine ammo by 1
			--print(firearm:WaitForChild("MagazineAmmo").Value)
			firearm:WaitForChild("MagazineAmmo").Value -= 1
			print(firearm:WaitForChild("MagazineAmmo").Value)

			wait(1 / (800 / 60))
			
			-- Raycast Bullet
			local rayOrigin = firearm:WaitForChild("BulletExit").Position
			local rayDirection = ((mousePos - rayOrigin).Unit) * 1000
			
			-- Remove muzzle from raycast instances
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {firearm:WaitForChild("BulletExit")}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			
			local bullet = Instance.new("Part")
			local speed = 500

			bullet.Name = "Bullet"
			bullet.Parent = game.Workspace
			bullet.Size = Vector3.new(0.25, 0.25, 0.25)
			bullet.BrickColor = BrickColor.new("New Yeller")
			bullet.Massless = true
			bullet.Anchored = true
			bullet.Material = Enum.Material.Neon
			bullet.CanCollide = false
			
			-- If there is a raycast result
			if raycastResult and raycastResult.Instance then
				-- Moves to the center of the two parts
				local center = (raycastResult.Position + rayOrigin) / 2
				-- Makes size of bullet big enough to stretch from origin to raycast
				bullet.Size = Vector3.new(0.05, 0.05, (rayOrigin - raycastResult.Position).Magnitude)
				bullet.CFrame = CFrame.lookAt(center, raycastResult.Position)
			else -- If there isn't a result (ex: skybox) shoot bullet into air (THIS ISNT WORKING)
				print("NOT HITTING")
				local center = (mousePos + rayOrigin) / 2 
				bullet.Size = Vector3.new(0.05, 0.05, (rayOrigin - mousePos).Magnitude)
				bullet.CFrame = CFrame.new(center, mousePos)
				--bullet.CFrame = CFrame.new(rayOrigin, mousePos) -- WORKS
			end
			
			-- Gradually erase the part starting from muzzle to end
			
			wait(0.05)
			bullet:Destroy()

The code that I’m having problems with is closer to the end of the code in the else statement.

if raycastResult and raycastResult.Instance then

try replacing this with

if raycastResult then

Use raycastResult.Position rather than Instance for position. If the ray hits nothing, it will return no instance.

also for bullet size replace

bullet.Size = Vector3.new(0.05, 0.05, (rayOrigin - mousePos).Magnitude)

with

bullet.Size = Vector3.new(0.05, 0.05, raycastResult.Distance)

1 Like

it won’t really matter because the script checks if raycast hit anything