Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't this work?

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.