How do i raycast effectively?

Dont really know how to use raycast
Set up some basic raycast thing from a tutorial and im wondering how i could stop the part from going through the wall as shown here

Also anyway to stop this weird delay?

And finally is there a way i can improve the ray so it doesnt look as choppy?

(Code)

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 * 50
	
	local Rr = workspace:Raycast(origin, dir, rayP)
	
	if Rr then
		print(Rr.Instance.Name)
		local sd = Instance.new("Part", workspace.BFolder)
		local midpoint = origin + dir/2
		
		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, dir.Magnitude)
		
		Debris:AddItem(sd, 0.1)
	end
end
1 Like

Hello,

I am just going to let you know that there is a module that many people use named FastCast. You do not need to switch over to this module but its there for if you need it:

It is built for making FPS type games. It has bullet drops, bullet velocity, less lag, more efficient. It is used for guns and it inserts parts into the workspace. You can customize these parts for if you have a different gun that shoots out different objects. There are many benefits to this module.

There is a tutorial on YouTube that shows you how to implement it as well:

i was orginally using fastcast for my guns, although i switched to raycast as i wanted my bullets to have a ray like feature
(in another post someone said raycast would be better at making ray type bullets)

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.