Make tracers appear instantly

Hello there!

I’m working on a shotgun for my FPS game, and I’m trying to make the tracers appear instantly, although the tracers can be seen moving point a to point b quickly.

(Shotgun Script, Server)

--//Item
local Tool = script.Parent
local TracerModule = require(workspace:WaitForChild("TracerModule"))
--//Func
Tool.FireEvent.OnServerEvent:Connect(function(Plr, MousePos)
	local Char = Plr.Character or Plr.CharacterAdded:Wait()
	local Hum = Char:FindFirstChildWhichIsA("Humanoid")
	--//Firing
	local FiringRayThread = coroutine.create(function()
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {HitPart, Char}
		
		for i=1, 8 do
			local pelletSpread = CFrame.new(Tool.Base.Position,MousePos) * CFrame.Angles(math.rad(math.random(-10, 10)), math.rad(math.random(-10, 10)), 0)
			local ray = Ray.new(Tool.Base.Position, pelletSpread.LookVector*500)

			local instance, pos = workspace:FindPartOnRay(ray, Char, false, true)
			if instance then
				local HitPart = Instance.new("Part",workspace)
				game:GetService("Debris"):AddItem(HitPart,3)
				HitPart.Size = Vector3.new(0.1,0.1,0.1)
				HitPart.Anchored = true
				HitPart.CanCollide = false
				HitPart.Position = pos
				
				TracerModule(Tool.Base.Position, HitPart.Position)

				local VicHum = instance.Parent:FindFirstChild("Humanoid")
				if VicHum then
					VicHum:TakeDamage(3.5)
				end
			end
		end
	end)
	coroutine.resume(FiringRayThread)
	
	--//Fire
	local FireAnim = Hum:WaitForChild("Animator"):LoadAnimation(Tool.Fire)
	FireAnim:Play()
	
	FireAnim:GetMarkerReachedSignal("FireGun"):Connect(function()
		Tool.Base.Fire:Play()
		Tool.Base.Attachment.Fog:Emit(math.random(30,60))
		Tool.Base.Attachment.PointLight.Enabled = true
		task.wait(.03)
		Tool.Base.Attachment.PointLight.Enabled = false
	end)
	FireAnim:GetMarkerReachedSignal("RackGun"):Connect(function()
		Tool.Base.Rack:Play()
	end)
end)

Tracer (Module 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

Maybe put this line into a task.defer()

What does that do?

dkdjidwjiqwejiweq filer gaming

Task.defer() is similar to task.spawn(), spawn() and coroutine.wrap(). Basically, create a thread which doesn’t yield your loop. Since that’s the reason. More specifically, this part:

Hmm okay, but how can I add the task function to the line of code you suggest adding

Instead of the line simply do

task.defer(function()
    -- TracerModule line
end)
1 Like

So I just paste the entirety of the module script code to there?

task.defer(function()
    local Tween = TS:Create(At0, TweenInfo.new(.1, 
    Enum.EasingStyle.Linear), {Position = At1.Position})
	Tween:Play()
	Tween.Completed:Wait()
	--CBullet:Destroy()
end)

Alright then, thanks!

njqwejiweqjwqeijqwe2

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.