how to make raycasting bullets stop? [closed]
Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).
I'm trying to make a bullet for a pistol I'm making. But IDK how to make it so that the bullet size stops at the hit target.
Script:
01 | local tool = script.Parent |
02 | local Handle = tool:WaitForChild( "Handle" ) |
03 | local Shoot = tool:WaitForChild( "Shoot" ) |
05 | local Debris = game:GetService( "Debris" ) |
10 | local headshotDamage = 40 |
12 | Shoot.OnServerEvent:Connect( function (Player, mousePos, originPos) |
13 | local Character = Player.Character |
14 | local Humanoid = Character:WaitForChild( "Humanoid" ) |
16 | local bulletDirection = (mousePos - originPos).unit * range |
17 | local midpointPos = originPos + bulletDirection / 2 |
19 | local raycastParams = RaycastParams.new() |
20 | raycastParams.FilterDescendantsInstances = { Character, workspace:WaitForChild( "Bullets" ) } |
21 | raycastParams.FilterType = Enum.RaycastFilterType.Blacklist |
23 | local raycastResults = workspace:Raycast(originPos, bulletDirection, raycastParams) |
25 | local bullet = Instance.new( "Part" , workspace:WaitForChild( "Bullets" )) |
26 | bullet.BrickColor = BrickColor.new( "Institutional white" ) |
27 | bullet.Material = "SmoothPlastic" |
28 | bullet.Name = Player.Name.. "'s bullet" |
29 | bullet.Transparency = 0.5 |
30 | bullet.Anchored = true |
32 | bullet.CanCollide = false |
33 | bullet.CFrame = CFrame.new(midpointPos, originPos) |
34 | bullet.Size = Vector 3. new(. 3 ,. 3 ,bulletDirection.magnitude) |
36 | Debris:AddItem(bullet, . 75 ) |
38 | if raycastResults then |
40 | local ECharacter = raycastResults.Instance.Parent |
41 | local EHumanoid = ECharacter:FindFirstChild( "Humanoid" ) |
43 | if EHumanoid and EHumanoid ~ = Character then |
44 | if raycastResults.Instance.Name = = "Head" or raycastResults.Instance:IsA( "Hat" ) then |
45 | EHumanoid:TakeDamage(headshotDamage) |
47 | EHumanoid:TakeDamage(damage) |
50 | print (EHumanoid.Health) |
54 | Shoot:FireClient(Player) |
Re-edited by Jessesong