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

How do I make this detect if a part doesn't contain humanoid?

Asked by 9 years ago

I am aware that this is a VERY inefficient script, but that's not why its here

local tool = script.Parent local player = game:GetService("Players").LocalPlayer local X = 0 local Debounce = false local Damage = 0 local Ammount = 0 local Conflict = false tool.Equipped:connect(function(mouse) print("Tool equipped!")

mouse.Button1Down:connect(function()
    print("Mouse pressed!")
    if Debounce == true then
        return
    end
    Debounce = true
    local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

    local Charge = Instance.new("Part", workspace)
    Charge.BrickColor = BrickColor.new("Toothpaste")
    Charge.Anchored = true
    Charge.Material = "Neon"
    Charge.Transparency = 0.5
    Charge.Locked = true
    Charge.CanCollide = false
    Charge.FormFactor = "Custom"
    Charge.CFrame = tool.Handle.CFrame
    Charge.Size = Vector3.new(1,1,1)
    local Mesh = Instance.new("SpecialMesh", workspace)
    Mesh.Parent = Charge
    Mesh.MeshType = "Sphere"
    Damage = math.random(10,20)
    Ammount = math.random(1,5)
    Mesh.Scale = Vector3.new(X,X,X)  -Repeats 40 times
    X = X + 0.2
    wait()


    local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("Toothpaste")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.25
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false

    local distance = (tool.Handle.CFrame.p - position).magnitude
    beam.Size = Vector3.new(0.5, 0.5, distance)
    beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
    if part then                                                            --Problem area
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            Ammount = 0
            Conflict = false
        end

        if humanoid then
            Conflict = true
        end
    else
        Ammount = 0
        Conflict = false
    end
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    beam.Size = Vector3.new(3, 3, distance)
    while Conflict == true and Ammount > 0 do
        wait(0.1)
        humanoid:TakeDamage(Damage)
        Ammount = Ammount - 1
    end
    wait(Ammount+1)
    Conflict = false
    beam.Size = Vector3.new(2.5, 2.5, distance)
    beam.Transparency = 0.375
    Charge.Transparency = 0.375
    wait()
    beam.Size = Vector3.new(2, 2, distance)
    beam.Transparency = 0.5
    Charge.Transparency = 0.5
    wait()
    beam.Size = Vector3.new(1.5, 1.5, distance)
    beam.Transparency = 0.675
    Charge.Transparency = 0.675
    wait()
    beam.Size = Vector3.new(1, 1, distance)
    beam.Transparency = 0.75
    Charge.Transparency = 0.75
    wait()
    beam:Destroy()
    Charge:Destroy()
    X = 0
    wait(5)
    Debounce = false
end)                                                                    --Problem Area End

end)

In short. The laser works if it hits a part containing Humanoid, but it doesn't work if it hits nothing or a part doesn't contain Humanoid

1
Please fix the formatting and read the instructions on how NOT to post a ques such as um... trying to post a huge script which would take us too long to pull apart, please do your best to locate the nearest 10-30 lines of code to the error dragonkeeper467 453 — 9y
0
https://scriptinghelpers.org/blog/posting-good-questions-and-answers-on-scripting-helpers <- here ya go! obey or leave cause we shouldn't have to keep repeating ourselves dragonkeeper467 453 — 9y

Answer this question