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

Why doesn't this work?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I scripted a gun. On the damage, I don't want it to kill myself. So I set the name of the bullets to the Players name(Owner). However, it does kill me. Any soloutions?

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Activated = true
script.Parent.Equipped:connect(function()
    Mouse.Icon = "http://www.roblox.com/asset/?id=59125633"
    Activated = false
end)
script.Parent.Unequipped:connect(function()
    Activated = true
end)
function Shoot()
    print("Shoot")
    if Mouse == Player:GetMouse() and script.Parent.Parent.Pistol ~= nil and Activated == false then
        local Bullet = Instance.new("Part", workspace)
        Bullet.FormFactor = "Custom"
        Bullet.Size = Vector3.new(0.3, 0.3, 0.3)
        Bullet.TopSurface = "Smooth"
        Bullet.Name = Player.Name
        Bullet.BottomSurface = "Smooth"
        Bullet.Shape = "Ball"
        Bullet.CFrame = script.Parent.Handle.CFrame
        Bullet.CFrame = CFrame.new(Bullet.Position, Mouse.Hit.p)
        Bullet:BreakJoints()
        Power = Instance.new("BodyVelocity", Bullet)
        Power.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        Power.Velocity = Bullet.CFrame.lookVector*1000
        Bullet.Touched:connect(function(Hit)
        if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Name ~= script.Parent.Name then
            Hit.Parent.Humanoid.Health = Hit.Parent.Humanoid.Health -40
        end
        if Hit.Parent:FindFirstChild("Head") and Hit.Parent.Name ~= script.Parent.Name then
            Hit.Parent.Humanoid.Health = 0
            end
        end)
    end
end
Mouse.Button1Down:connect(Shoot)

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

You can check to see if the part that was hit is a descendant of the player's character:

local player = game.Players.LocalPlayer

Bullet.Touched:connect(function(hit)
    if not hit:IsDescendantOf(player.Character) then
        -- code
    end
end)
Ad

Answer this question