Tool Kills Myself When Activated

I have adapted this ban hammer tool and rescripted it almost all myself to make it so it kills a player on the animation. The animation on click makes the hammer strike down in front of me and find a player and makes its health go down a little at a time. However, when I click, it lowers my own health. Again, I adapted this tool and rescripted most of it so I’m not completely sure where the problem is. Here is the main local script. Tell me if you find any problems. Thanks!

player = nil
--selected
selected = false
homerunning = false 
whacking = false 
--save shoulders
RSH, LSH = nil, nil
anim = "none"
--grip
--welds
--what anim
local TwoHandAnim
local HomeRunner
local Whacker
local config = false
local user
local damage = 15
--create the dustcloud object
local prt = Instance.new("Part")
prt.Size = Vector3.new(7.5,7.5,7.5)
prt.BrickColor = BrickColor.new(217)
prt.Anchored = true
prt.CanCollide = false
prt.Transparency = 1
smoke=Instance.new("Smoke", prt)
smoke.Color = BrickColor.new("Brown").Color
smoke.RiseVelocity = -2
smoke.Size = 4.5
smoke.Opacity = 1
--set variables
hammer = script.Parent.Handle
--[[local msh = Instance.new("SpecialMesh")
msh.MeshId = "http://www.roblox.com/asset/?id=1095708"
msh.MeshType = 5
msh.Parent = prt]]

----- show version ----------
script.Parent.Name = "BanHammer V1.1"
---------------------------------

function GetFigures(Parent, Part)
	for i, v in pairs(Parent:GetChildren()) do 
		GetFigures(v, Part)
		if v.className == "Humanoid" then 
			if v == script.Parent.Parent:findFirstChild("Humanoid") == nil then 
				if v.Parent:findFirstChild("Torso") then 
					if v.Parent:findFirstChild("Torso"):IsA("BasePart") then 
						if v.Parent:findFirstChild("Torso").Position.x < Part.Position.x + Part.Size.x / 2 
						and v.Parent:findFirstChild("Torso").Position.x > Part.Position.x - Part.Size.x / 2 
						and v.Parent:findFirstChild("Torso").Position.y < Part.Position.y + Part.Size.y / 2 
						and v.Parent:findFirstChild("Torso").Position.y > Part.Position.y - Part.Size.y / 2 
						and v.Parent:findFirstChild("Torso").Position.z < Part.Position.z + Part.Size.z / 2 
						and v.Parent:findFirstChild("Torso").Position.z > Part.Position.z - Part.Size.z / 2 then 
							v.Sit = true 
						end 
						if (Part.Position - v.Parent:findFirstChild("Torso").Position).magnitude < 10 then 
							v.Sit = true 
						end 
					end 
				end 
			end 
		end 
	end 
end 
function Raycast(Pos, Dir, Max, Ignore)
return game:GetService("Workspace"):FindPartOnRay(Ray.new(Pos, Dir.unit * (Max or 999.999)), Ignore) 
end 
function WaitForChild(obj, ch_n)
	local t = time()
	while not obj:FindFirstChild(ch_n) and time() - t < 10 do wait(0.1) end
	return obj:FindFirstChild(ch_n)
end

--onselected, save shoulders and get player
script.Parent.Equipped:connect(function(mouse)
	user=game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	TwoHandAnim = script.Parent.Parent:findFirstChild("Humanoid"):LoadAnimation(script.Parent.TwoHandAnim)
	HomeRunner = script.Parent.Parent:findFirstChild("Humanoid"):LoadAnimation(script.Parent.HomeRun)
	Whacker = script.Parent.Parent:findFirstChild("Humanoid"):LoadAnimation(script.Parent.Whack)
	repeat wait() until TwoHandAnim ~= nil and HomeRunner ~= nil and Whacker ~= nil 
	if selected then return end
	selected = true
	TwoHandAnim:Play()
	mouse.KeyDown:connect(function(key)
		if key=="q" then
			config=false
		elseif key=="e" then
			config=true
		end
	end)
end)

--griptcf = CFrame.new(0, -1, 0) * CFrame.fromEulerAnglesXYZ(-math.pi/2+0.5, 0, 0)

script.Parent.Unequipped:connect(function()
	selected = false
	if TwoHandAnim ~= nil then 
		TwoHandAnim:Stop()
	end 
	if HomeRunner ~= nil then 
		HomeRunner:Stop()
	end 
	if Whacker ~= nil then 
		Whacker:Stop()
	end 
end)

function HomeRunHit(part)
	if part == nil then return end 
	if part.Parent == nil then return end 
	local h = (part.Parent or game):FindFirstChild("Humanoid") --or findfirstchild optimization
	if h then
		if h == script.Parent.Parent:findFirstChild("Humanoid") == false then 
			--game.SoundService.Bomb:Play()
	--		hammer.Homerun:Play()
			hrs = Instance.new("StringValue")
			hrs.Name = "HomerunSound"
			hrs.Parent = hammer
			h.Sit = true
			h.Parent.Torso.Velocity = (CFrame.new(hammer.Position, h.Parent.Torso.Position).lookVector * 200) + Vector3.new(0, 100, 0)
			h.Parent.Torso.RotVelocity = Vector3.new(math.random(-100, 100), math.random(-100, 100), math.random(-100, 100))
		end 
	end
end

function tk(hp)
	if (hp~=nil and hp.Parent~=nil and hp.Parent:FindFirstChild("Humanoid") and hp.Name~="Bullet" and hp.Name~="Handle") then
		local human=hp.Parent:FindFirstChild("Humanoid")
		human:TakeDamage(tonumber(damage))
	end
end

function HomeRun()
	if homerunning == false then 
		homerunning = true 
		anim = "homerun"
		TwoHandAnim:Stop()
		HomeRunner:Play()
		--start HomeRunhit connection--
		local con
		if config==true then
			con=hammer.Touched:connect(tk)
		else
			con=hammer.Touched:connect(HomeRunHit)
		end
		----------------------------------------
		wait(.6)
		HomeRunner:Stop()
		if selected == true then 
			TwoHandAnim:Play()
		end 
		----end HomeRun connection---
		con:disconnect()
	--	wait(0.15)
		homerunning = false 
	end
end 
function Whack()
	if homerunning == false then 
	anim = "norm"
	TwoHandAnim:Stop()
	Whacker:Play()
--	hammer.Whack:Play()
	local con=hammer.Touched:connect(tk)
		--add dust cloud
		delay(0.25, function()
			if anim == "norm" then 
				if selected == true then 
					whacking = true 
					p=Instance.new("Part")
					p.CanCollide = false 
					p.Transparency = 1
					p.Parent = script.Parent.Parent
					p.formFactor = "Custom"
					p.Size = Vector3.new(0.2, 0.2, 0.2)
					p.Anchored = true
					p.CFrame = hammer.CFrame - Vector3.new(0, script.Parent.Parent:findFirstChild("Torso").Size.y/2, 0) + script.Parent.Parent:findFirstChild("Torso").CFrame.lookVector * ((hammer.Size.y / 2) - p.Size.z)
					p.CFrame = CFrame.new(p.CFrame.p) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0))
					p.CFrame = p.CFrame * CFrame.fromEulerAnglesXYZ(-math.pi/2, 0,  -math.pi/2)
					Hit, Pos = Raycast(p.Position, p.CFrame.lookVector, 4, script.Parent.Parent)
					if Hit ~= nil then 
						if homerunning == false then 
							delay(0.35, function()
								w = Instance.new("StringValue")
								w.Name = "WhackSound"
								w.Parent = hammer
							end)
							local pt = prt:clone()
							pt.Parent = script.Parent.Parent
							pt.CFrame = CFrame.new(hammer.CFrame.x, Pos.y + 0.75, hammer.CFrame.z) + script.Parent.Parent:findFirstChild("Torso").CFrame.lookVector * 1.75
							GetFigures(pt.Parent.Parent, pt)
							game.Debris:addItem(pt, 1.3)
						end 
					end 
					p:Remove()
				end 
			end 
		end)
		wait(0.75)
		con:disconnect()
		Whacker:Stop()
		whacking = false 
		------
		if selected == true then 
			TwoHandAnim:Play()
		end 
	end 
end 
local a = false
local co = nil
--OMGHAX mouseclick
local last_click = 0
script.Parent.MouseClick.Changed:connect(function()
	if homerunning == false and whacking == false then 
		if time() - last_click < 0.3 then
			anim = "homerun"
			last_click = time()
			HomeRun()
		else
			anim = "norm"
			last_click = time()
			Whack() 
		end 
	end 
end)

If a line can be provided on where to fix that would be great.

1 Like

First, make a reference to the LocalPlayer.
local LocalPlayer = game:GetService("Players").LocalPlayer
and you can change the tk function to this.

function tk(hp)
	local ret = (hp == nil) or (not hp.Parent)
    if (ret) then return end
    local targetHumanoid = hp.Parent:FindFirstChildOfClass("Humanoid")
    if (not targetHumanoid) then return end
    local lpCharacter = LocalPlayer.Character
    if ((not lpCharacter) or (lpCharacter == targetHumanoid.Parent)) then return end
    targetHumanoid:TakeDamage(damage)
end

This should stop the error because it checks to see who is being damaged by comparing the Characters to eachother.

This worked, but now the player only dies on my screen because this is a local script. Where should I insert a remoteevent to send to a regular script in the tool in order to actually have the player killed successfully?

Hi! Believe it or not you can put everything in the tool itself.
Add a RemoteEvent into the tool. Call it “Damage”.
Add a script into the tool also.
In the localscript, make a reference to the RemoteEvent.
local damageRemote = script.Parent:WaitForChild(“Damage”)
Change the current tk function to this:

function tk(hp)
	local ret = (hp == nil) or (not hp.Parent)
    if (ret) then return end
    local targetHumanoid = hp.Parent:FindFirstChildOfClass("Humanoid")
    if (not targetHumanoid) then return end
    local lpCharacter = LocalPlayer.Character
    if ((not lpCharacter) or (lpCharacter == targetHumanoid.Parent)) then return end
    damageRemote:FireServer(targetHumanoid, damage)
end

Regular script

local event = script.Parent:WaitForChild(“Damage”)
event.OnServerEvent:Connect(function(player, humanoid, damage)
    if humanoid then
        humanoid:TakeDamage(damage)
    end
end)

Please keep in mind that I have not tested any of this.
You may want to change the client-side checks to server-sided checks.
Keep in mind that the localplayer is automatically passed as the first parameter with FireServer().
Let me know how it goes.
If I helped you please consider marking as solution.
:slight_smile: