How do i raycast effectively?

If you mean the yellow bullet that comes out of the gun, then you can just use fastcast, but with a beam and two attachments instead.

Beams are also beneficial. They can stop that delay with LockedToPart.
However, it will follow the gun with LockedToPart, you should just use a really quickly destroying beam since most bullets IRL are supersonic and can’t really be seen by the human eye. A great reference would be FE gun kit’s bullets.

Yes. As @no_tsi mentioned you can use bullet trails. You make 2 attachments and attach a trail to it.

The only way for the raycast to clip through the walls is for rayP to somehow whitelist that piece. Could you share what rayP is?
P.S: Using abbreviated variable names makes you code hard to read for people who don’t know where they came from.

1 Like

its just raycastParams
and all i have it to is

local function onEquipped()
	local FilterTable = {}
	tool.Folder.Grip["AK47 Equip"]:Play()
	for i, v in pairs(game.Workspace.Mobs:GetChildren()) do
		if v:IsA("Model") and v:FindFirstChildWhichIsA("Humanoid") then
			for _, v2 in pairs(v:GetChildren()) do
				if v2:IsA("Accessory") then
					table.insert(FilterTable, v2)
				end
			end
		end
	end
	table.insert(CT, tool.Parent)
	table.insert(CT, BFolder)
	table.insert(CT, game.Workspace.Extras)
	table.insert(CT, plrfolder)
	print(CT)
	rayP.FilterDescendantsInstances = CT
end

ive tried doing this before, but whenever i return the bullet to partcache, the beam creates a giant thing to the top where the partchace is located

Yes I understand you, I had the same issue. This might help out though:

You could just make the bullet speed really really really fast so it’s basically hitscan

hmm ill check it out, may be the fix i could of used 2 months ago

they wouldn’t be able to see the bullet tho, even then id have to make big bullets which wouldn’t even work with different ranges

Oh true, my bad. I should have thought of that. Why don’t you raycast then you make your part from your hit?

Also the delay is the client server replication lag

We can stop clipping via utilizing the .Distance property of a RaycastResult:

local TS = game:GetService("TweenService")

local function fire(player, mPos)
	
	if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
	shotTime = tick()
	
	if AmmoLeft.Value < 0 then return end
	
	local ShotSound = tool.Barrel.AK47
	local fA = script.Parent.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(script.Parent.Ak47Fire)
	fA:Play()
	local DA = script.Parent.Parent:FindFirstChild("Humanoid")
	
	if not DA then return end
	if DA.Health <= 0 then return end
	
	local origin = tool.Barrel.Origin.Position
	local range = 50
    local dir = (mPos - tool.Barrel.Origin.Position).Unit

    local Rr = workspace:Raycast(origin, range * dir, rayP)

	if Rr then
		local sd = Instance.new("Part", workspace.BFolder)
		local midpoint = origin + Rr.Distance / 2 * dir
		
		sd.Material = Enum.Material.Neon
		sd.BrickColor = BrickColor.new("Yellow flip/flop")
		sd.Anchored = true
		sd.CanCollide = false
		sd.CFrame = CFrame.new(midpoint, origin)
		sd.Size = Vector3.new(.1,.1, Rr.Distance)
		
		Debris:AddItem(sd, 0.1)
	end
end

In addition, my attempt to make it less choppy, we can alternatively do :

if Rr then
    local sd = Instance.new("Part")
    local midpoint = origin + Rr.Distance / 2 * dir
    
    sd.Material = Enum.Material.Neon
    sd.BrickColor = BrickColor.new("Yellow flip/flop")
    sd.Anchored = true
    sd.CanCollide = false
    sd.CFrame = CFrame.new(origin, origin - dir)
    sd.Size = Vector3.new(.1,.1, .1)
    sd.Transparency = 0
    sd.Parent = workspace.BFolder

    -- Alternative approach to reduce the "choppiness"

    local fadeInTween = TS:Create(sd, TweenInfo.new(.03), {
        Transparency = 0,
        Size = Vector3.new(.1, .1, Rr.Distance)
        CFrame = CFrame.new(midpoint, origin)
    })
    fadeInTween.Play()
    fadeInTween.Completed:Wait()

    local fadeOutTween = TS:Create(sd, TweenInfo.new(.07), {
        Transparency = 1,
    })
    fadeOutTween.Play()
    fadeOutTween.Completed:Wait()

    sd:Destroy()
end

(Code has not been tested and may error / bug, just let me know and i’ll correct it)

Is there anyway to fix this or do i just have to stick with it

ill take a look at this code and let you know how it goes

I editted a couple more things. You’re not caching anything and your approach to getting the humanoid and everything is very strange, so I fixed those too. Also, I got rid of the fadeIn, and just left the fadeOut. This may look better:

local TS = game:GetService("TweenService")

-- Cache things. This greatly improves performance...

local scriptParentThing = script.Parent

local function fire(player, mPos)
	if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
	shotTime = tick()
	
	if AmmoLeft.Value <= 0 then return end
	
	local ShotSound = tool.Barrel.AK47

    local humanoid = player.Character:FindFirstChild("Humanoid")

    if not humanoid then return end
    if humanoid.Health <= 0 then return end

	local fA = humanoid:WaitForChild("Animator"):LoadAnimation(scriptParentThing.Ak47Fire)
	fA:Play()
	
	local origin = tool.Barrel.Origin.Position
	local range = 50
    local dir = (mPos - tool.Barrel.Origin.Position).Unit

    local Rr = workspace:Raycast(origin, range * dir, rayP)

	if Rr then
        local sd = Instance.new("Part")
        local midpoint = origin + Rr.Distance / 2 * dir
        
        sd.Material = Enum.Material.Neon
        sd.BrickColor = BrickColor.new("Yellow flip/flop")
        sd.Anchored = true
        sd.CanCollide = false
        sd.Size = Vector3.new(.1, .1, Rr.Distance)
        sd.CFrame = CFrame.new(midpoint, origin)
        sd.Parent = workspace.BFolder
    
        task.wait(.05)

        local fadeOutTween = TS:Create(sd, TweenInfo.new(.05), {
            Transparency = 1,
        })

        fadeOutTween:Play()
        fadeOutTween.Completed:Wait()
    
        sd:Destroy()
	end
end
1 Like

Alright heres how it looks now

Still a little iffy, not sure what else can be done to it

Also haven’t been caching things yet, mainly due to just trying to get the gun to work decently
(Now that i look at this its dumb i know)

Here’s another revision:

local TS = game:GetService("TweenService")

-- Cache things. This greatly improves performance...

local scriptParentThing = script.Parent

local function fire(player, mPos)
	if not ((tick()-shotTime) >= fireRate*timeLimit) then return end
	shotTime = tick()
	
	if AmmoLeft.Value <= 0 then return end
	
	local ShotSound = tool.Barrel.AK47

    local humanoid = player.Character:FindFirstChild("Humanoid")

    if not humanoid then return end
    if humanoid.Health <= 0 then return end

	local fA = humanoid:WaitForChild("Animator"):LoadAnimation(scriptParentThing.Ak47Fire)
	fA:Play()
	
	local origin = tool.Barrel.Origin.Position
	local range = 50
    local dir = (mPos - tool.Barrel.Origin.Position).Unit

    local Rr = workspace:Raycast(origin, range * dir, rayP)

	if Rr then
        local sd = Instance.new("Part")
        local midpoint = origin + Rr.Distance / 2 * dir
        
        sd.Material = Enum.Material.Neon
        sd.BrickColor = BrickColor.new("Yellow flip/flop")
        sd.Anchored = true
        sd.CanCollide = false
        sd.Size = Vector3.new(.1, .1, Rr.Distance)
        sd.CFrame = CFrame.new(midpoint, origin)
        sd.Parent = workspace.BFolder
    
        task.wait(.05)

        local fadeOutTween = TS:Create(sd, TweenInfo.new(.05), {
            Transparency = 1,
            CFrame = CFrame.new(origin + Rr.Distance * dir, origin),
            Size = Vector3.new(.1, .1, .1)
        })

        fadeOutTween:Play()
        fadeOutTween.Completed:Wait()
    
        sd:Destroy()
	end
end

Now the ray part will shrink towards the target on fade out - that should look better.

Heres it now, i dont think the tween is slow enough as you can barley see it shrink down

Also dont know when it was added, but the bullets no longer go through walls

The tool in this video almost has the same effect as your gun

It might help you make a response beam without lag and stutter

In your first post the reason that you beam is slow like that is because your positioning it on the server side and there is a delay in the character position on the server side because it takes time for the client to send the characters position to the server

maybe, not to sure although

(Char limit)