Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to make raycasting bullets stop? [closed]

Asked by 3 years ago
Edited by JesseSong 3 years ago

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:

local tool = script.Parent
local Handle = tool:WaitForChild("Handle")
local Shoot = tool:WaitForChild("Shoot")

local Debris = game:GetService("Debris")

local range = 100

local damage = 20
local headshotDamage = 40

Shoot.OnServerEvent:Connect(function(Player, mousePos, originPos)
    local Character = Player.Character
    local Humanoid = Character:WaitForChild("Humanoid")

    local bulletDirection = (mousePos - originPos).unit * range
    local midpointPos = originPos + bulletDirection / 2

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {Character, workspace:WaitForChild("Bullets")}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResults = workspace:Raycast(originPos, bulletDirection, raycastParams)

    local bullet = Instance.new("Part", workspace:WaitForChild("Bullets"))
    bullet.BrickColor = BrickColor.new("Institutional white")
    bullet.Material = "SmoothPlastic"
    bullet.Name = Player.Name.."'s bullet"
    bullet.Transparency = 0.5
    bullet.Anchored = true
    bullet.Locked = true
    bullet.CanCollide = false
    bullet.CFrame = CFrame.new(midpointPos, originPos)
    bullet.Size = Vector3.new(.3,.3,bulletDirection.magnitude)

    Debris:AddItem(bullet, .75)

    if raycastResults then

        local ECharacter = raycastResults.Instance.Parent
        local EHumanoid = ECharacter:FindFirstChild("Humanoid")

        if EHumanoid and EHumanoid ~= Character then
            if raycastResults.Instance.Name == "Head" or raycastResults.Instance:IsA("Hat") then
                EHumanoid:TakeDamage(headshotDamage)
            else
                EHumanoid:TakeDamage(damage)
            end

            print(EHumanoid.Health)
        end
    end

    Shoot:FireClient(Player)
end)

Re-edited by Jessesong

Closed as Not Constructive by JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?