1

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)
takezo
  • 101
  • 7

2 Answers2

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

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

MobiDev
  • 154
  • 5
  • i forgot the hsit.Parent only, but thank you – takezo Jul 23 '22 at 13:18
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '22 at 18:45
0
if hsit:FindFirstChild("Humanoid") ~= Player.Character.Humanoid then

change this to

if hsit.Parent and hsit.Parent:FindFirstChild('Humanoid') ~= Player.Character.Humanoid then
ZygD
  • 22,092
  • 39
  • 79
  • 102
DevAX1T
  • 16