takezo
takezo

Reputation: 91

How to prevent the Player humanoid to die from a part

I made a pistol that fires a part and if a humanoid touches the part then he takes damage but I don't want the player to die when he touches that part, I already tried using the ~= operator but it didn't work. here is the code

local db = false

game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(Player, Hit)
    local Shot = Instance.new("Part")
    Shot.BrickColor = BrickColor.new("New Yeller")
    Shot.CanCollide = false
    local BackPack = Player.Character
    Shot.Position = BackPack.Tool.Part.Position
    Shot.Shape = Enum.PartType.Block
    Shot.Size = Vector3.new(0.34, 0.228, 2)
    local VectorForce = Instance.new("VectorForce")
    local Attach = Instance.new("Attachment", Shot)
    VectorForce.Attachment0 = Attach
    VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
    VectorForce.Force = CFrame.lookAt(BackPack.Tool.Part.Position, Hit.Position).LookVector * 500
    VectorForce.Parent = Shot
    if not db then
        db = true
        Shot.Touched:Connect(function(hsit)
            if hsit:FindFirstChild("Humanoid") ~= Player.Character.Humanoid then
                hsit.Parent.Humanoid.Health = hsit.Parent.Humanoid.Health-5
                task.wait(2)
                db = false
            end
        end)
    end
    Shot.Parent = workspace
    game.Debris:AddItem(Shot, 3)
    task.wait(2)
    db = false
end)

Upvotes: 1

Views: 350

Answers (2)

DevAX1T
DevAX1T

Reputation: 16

if hsit:FindFirstChild("Humanoid") ~= Player.Character.Humanoid then

change this to

if hsit.Parent and hsit.Parent:FindFirstChild('Humanoid') ~= Player.Character.Humanoid then

Upvotes: 0

MobiDev
MobiDev

Reputation: 466

if Player.Character.Humanoid.Health > 5 then
    -- damage player
end

this should work. Replace 5 with a variable reference or the damage constant.

Upvotes: 0

Related Questions