I’m making a bullet system where the player can shoot through walls to hit other players however the bullet is inaccurate and doesn’t follow a straight path
local module = {}
local aliveTime = 1
local raycastModulePTP = require(game.ServerStorage.Server_Modules:FindFirstChild("Raycast(PartToPart)"))
local raycastModuleDir = require(game.ServerStorage.Server_Modules:FindFirstChild("Raycast(Direction)"))
local dbs = game:GetService("Debris")
local function DetectCamera(part)
if part:GetAttribute("Camera") then
part.Parent.Parent:FindFirstChild("Break").Value = true
end
end
local function EffectHumanoid(firer, Bullet, damage)
local Player = firer
local touchingParts = game:GetService("Workspace"):GetPartsInPart(Bullet)
print(touchingParts)
for _, part in ipairs(touchingParts) do
if part.Parent:FindFirstChild("Humanoid") then
local humanoid = part.Parent:FindFirstChild("Humanoid")
humanoid.Health -= damage
print(part.Parent.Name.. " Has been hit")
print("Rig health: ".. humanoid.Health)
end
end
end
local function Detect(Bullet, firer, damage,range,gun, from, direction)
local touchingParts = game:GetService("Workspace"):GetPartsInPart(Bullet)
print(touchingParts)
EffectHumanoid(firer,Bullet, damage)
for _, part in ipairs(touchingParts) do
DetectCamera(part)
if part:GetAttribute("NBC") then
local ThroughMarker = Instance.new("Part")
ThroughMarker.Name = "Passthrough"
ThroughMarker.Size = Bullet.Size
ThroughMarker.Color = Color3.fromRGB(255, 225, 0)
ThroughMarker.Transparency = 1
ThroughMarker:SetAttribute("BulletBox", true)
ThroughMarker.Position = Bullet.Position
ThroughMarker.Orientation = Bullet.Rotation
ThroughMarker.Shape = Bullet.Shape
ThroughMarker.Anchored = true
ThroughMarker.Parent = game.Workspace.Ignored_Parts
dbs:AddItem(ThroughMarker, aliveTime)
local params = RaycastParams.new()
params.IgnoreWater = true
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace.Ignored_Parts, firer.Character, gun, part}
local ThroughBullet = Instance.new("Part")
ThroughBullet.Name = "Passthrough bullet"
ThroughBullet.Anchored = true
ThroughBullet.CanCollide = false
ThroughBullet.Transparency = 0
ThroughBullet:SetAttribute("BulletBox", true)
ThroughBullet.Color = Color3.fromRGB(0, 255, 0)
ThroughBullet.Size = Bullet.Size * 5
ThroughBullet.Shape = Bullet.Shape
print(tostring(from))
print(tostring(direction))
ThroughBullet.Position = raycastModuleDir.Raycast(from, direction, params, range).Position
ThroughBullet.Parent = game.Workspace.Ignored_Parts
EffectHumanoid(firer, ThroughBullet, damage / 2)
dbs:AddItem(ThroughBullet, aliveTime)
end
end
end
function module.FireBullet(Firer, gun, To, From, BulletSize, damage, range)
local BulletHitbox = Instance.new("Part")
BulletHitbox.Anchored = true
BulletHitbox.CanCollide = false
BulletHitbox.Transparency = 1
BulletHitbox.Color = Color3.fromRGB(0, 0, 0)
BulletHitbox.Size = Vector3.new(BulletSize, BulletSize, BulletSize)
BulletHitbox.Shape = Enum.PartType.Ball
local damageINT = Instance.new("IntValue")
damageINT.Value = tonumber(damage)
damageINT.Name = "Damage"
damageINT.Parent = BulletHitbox
BulletHitbox:SetAttribute("BulletBox", true)
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {workspace.Ignored_Parts, Firer.Character, gun}
print(To)
local intersection = raycastModulePTP.Raycast(From,To.Position, params, range).Position
BulletHitbox.Position = intersection
local part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(0.1,0.1,0.1)
part.Position = To.Position
part.Anchored = true
part.Color = Color3.fromRGB(0,255,0)
part.Parent = workspace.Ignored_Parts
BulletHitbox.Parent = game.Workspace.Ignored_Parts
dbs:AddItem(BulletHitbox, aliveTime)
Detect(BulletHitbox, Firer, damageINT.Value,range,gun, intersection, To.Position)
end
return module