Tracer not appearing whenever shooting at the sky

Heya there.

Remaking a ROBLOX game called Noobs vs Zombies: Relish Reborn. When making the default secondary, the tracer only appears whenever hitting something and not when aiming at the sky.

Server Script

--//Tool
local Tool = script.Parent
local FireEvent = Tool:WaitForChild("FireEvent")
--Attributes
local AmmoHold = Tool:GetAttribute("Ammo_Hold")
local AmmoContain = Tool:GetAttribute("Ammo_Contain")
local Cooldown = Tool:GetAttribute("Cooldown")

--//Functionality
local CollectionService = game:GetService("CollectionService")
FireEvent.OnServerEvent:Connect(function(Player, Type, MouseHit)
	local FirePos = Tool.Pistol.Position
	
	if Type == "Fire" then
		local ammoHold = Tool:GetAttribute("Ammo_Hold")
		--//Getting Players' team & role.
		local Self_Team = Player.Team
		local TagRole = CollectionService:GetTags(Player.Character)

		--//Sanity Checking
		if ammoHold <= AmmoHold then --Checking to see if the amount of ammo their holding is legit.
			Tool:SetAttribute("Ammo_Hold", ammoHold - 1)

			--//Firing the gun
			Tool.Pistol.Fire:Play()
			local RayParams = RaycastParams.new()
			RayParams.FilterDescendantsInstances = {Player.Character}
			RayParams.FilterType = Enum.RaycastFilterType.Exclude

			local Raycast = workspace:Raycast(Tool.Pistol.Position, (MouseHit - Tool.Pistol.Position).Unit * 500, RayParams)
			
			if Raycast then
				local PossiblePlayer = Raycast.Instance.Parent
				
				--//Creating bullet tracer
				local Direction = Raycast.Position
				game:GetService("ReplicatedStorage").ReplicateGunTracer:FireAllClients(FirePos,Direction)
				
				--//Checking if ray hits a player or character
				local CheckPlayer = game:GetService("Players"):GetPlayerFromCharacter(PossiblePlayer)
				if CheckPlayer then
					local PossibleEnemyTeam = CheckPlayer.Team
					if PossibleEnemyTeam ~= Self_Team then
						CheckPlayer.Character.Humanoid:TakeDamage(12)
					else
						--If we hit an ally.
						return
					end
				else --//If the ray a NPC instead, we can just check its tag/role to see if they're an ally or not.
					local PossibleEnemyNPC = Raycast.Instance.Parent
					if CollectionService:HasTag(PossibleEnemyNPC, "Noob") then
						--If we hit an ally.
						return
					end
					PossibleEnemyNPC:FindFirstChildWhichIsA("Humanoid"):TakeDamage(12)
				end
			else
				--//Creating bullet tracer
				local Direction = Tool.Pistol.Position + (MouseHit - Tool.Pistol.Position).Unit * 500
				game:GetService("ReplicatedStorage").ReplicateGunTracer:FireAllClients(FirePos,Direction)
			end
		else -- No need to elseif unless you're chaining a bunch of conditions. Either they have ammo or they don't here. 
			warn("Not valid. Stop hacking, cheater.")
			Tool:SetAttribute("Ammo_Hold", ammoHold - 1)
		end
	end
	--
	if Type == "Reload" then
		local ammoHold = Tool:GetAttribute("Ammo_Hold")
		local ammoContain  = Tool:GetAttribute("Ammo_Contain")
		
		--//Sanity checking
		if ammoHold <= AmmoHold then
			Tool:SetAttribute("Ammo_Hold", 12)
			Tool:SetAttribute("Ammo_Contain", ammoContain - 12)
		else
			warn("Too much ammo. Stop hacking, cheater.")
			return
		end
	end
end)

Module Script/Tracer Script

local TS = game:GetService("TweenService")

local defaultSettings = {
	TextureSpeed = 0,
	TextureLength = 1,
	Texture = "rbxassetid://10822615828",
	
	Color = ColorSequence.new(Color3.new(255, 170, 0)),
	Width0 = 0.4,
	Width1 = 0.4,
	
	Transparency = NumberSequence.new(0),
	LightEmission = 1,
	FaceCamera = true
}

return function(Source, Point, Settings)
	local Direction = (Source - Point).Unit
	local Distance = (Source - Point).Magnitude
	
	local CBullet = Instance.new("Part")
	CBullet.Anchored = true
	CBullet.CanCollide = false
	CBullet.Size = Vector3.new(0.1, 0.1, 0.1)
	CBullet.Transparency = 1
	CBullet.Parent = workspace.Debris
	
	CBullet.CFrame = CFrame.lookAt(Source, Point)
	
	local At0 = Instance.new("Attachment")
	At0.Position = Vector3.new(0, 0, 0)
	At0.Parent = CBullet
	
	local At1 = Instance.new("Attachment")
	At1.Position = Vector3.new(0, 0, -Distance)
	At1.Parent = CBullet
	
	local Beam = Instance.new("Beam")
	Beam.Attachment0 = At0
	Beam.Attachment1 = At1
	Beam.Parent = CBullet
	for property, value in pairs(defaultSettings) do
		Beam[property] = value
	end
	
	local Tween = TS:Create(At0, TweenInfo.new(.1, Enum.EasingStyle.Linear), {Position = At1.Position})
	Tween:Play()
	Tween.Completed:Wait()
	--CBullet:Destroy()
end

From your code:

if Raycast then
				local PossiblePlayer = Raycast.Instance.Parent
				
				--//Creating bullet tracer
				local Direction = Raycast.Position
				game:GetService("ReplicatedStorage").ReplicateGunTracer:FireAllClients(FirePos,Direction)

It looks like you are only creating a path if your raycast hits something, which won’t happen when you cast at the sky. You can create your tracer with the same direction of your cast even if you don’t hit anything.

1 Like

Hello, your issue of not seeing the bullet trace lies here. When getting Mouse.Hit when the mouse is pointed towards the “void/sky”, there is no part that the ray can hit to detect the hit position. This causes the position of Mouse.Hit to be very far when the mouse is directed at the void/sky. The reason why this is significant is because the only way you are interpolating between the fire and hit points is through a set duration tween. That means the trace will always hit the target position in 0.1 seconds.

To fix this, you could change the duration of the tween based on a bullet speed you determine (eg. 100 studs/s). Duration would then equal: distance/speed.

Seemed to work! Thanks you for the help!!