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.
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.
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
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)
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
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.
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