so i have a gun and the bullet uses used body velocity to make it hit
the part, but the thing is it goes though walls.
here a clip
and the code its in server script
gunevent.OnServerEvent:Connect(function(Player, Start, End)
---------Params
local function SetRaycastParams()
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {char:GetDescendants(), tool}
return params
end
local params = SetRaycastParams()
-----------Distance
local function calcDir()
local Orgian = Start
local dir = End - Orgian
local limit = (End - Start).Unit * 200
return Start, limit, dir
end
-------------Bullet
local function rayVis(origin, respos, d, result)
local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local dist = (origin - respos).Magnitude
local part = game:GetService("ReplicatedStorage"):WaitForChild("Bullet"):Clone()
part.Parent = result.Instance
part.Position = result.Position
part.CFrame = CFrame.new(origin, respos)
local bv = Instance.new("BodyVelocity")
bv.Parent = part
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = part.CFrame.LookVector * 70
debris:AddItem(part, 2)
end
-------------Bullet hole
local function bullehole(result)
local hole = replicates:WaitForChild("Bullehole", workspace.Ignore):Clone()
hole.Anchored = true
hole.CanCollide = false
hole.Parent = result.Instance
hole.Position = result.Position
hole.CFrame = CFrame.new(hole.Position, hole.Position+ result.Normal)
local wc = Instance.new("WeldConstraint", hole)
wc.Part1 = result.Instance
wait(2)
game:GetService("TweenService"):Create(hole.Decal, TweenInfo.new(0.5), {Transparency = 1}):Play()
task.delay(1, function()
game:GetService("TweenService"):Create(hole, TweenInfo.new(2), {Size = Vector3.new(0.97, 0.886, 0.009)}):Play()
game:GetService("TweenService"):Create(hole.Decal, TweenInfo.new(0.5), {Transparency = 1}):Play()
game:GetService("Debris"):AddItem(hole, 1)
end)
end
------------Raycast
local function ray()
local Orgian, dir = calcDir()
local result = _Workspace:Raycast(Orgian, dir, params) or (Orgian + dir)
if result then
print(result)
local hit = result.Instance
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
local Head = hit.Parent:FindFirstChild("Head")
if Humanoid then
if hit.Name == "Head" then
Humanoid.Health -= 50
else
Humanoid.Health -=10
end
end
end
if not result then return end
rayVis(Orgian, result.Position, dir, result )
bullehole(result)
print("Bullet created")
end
ray()
end)```