I want to make the hammer only damage when the mouse is down and to detect whether something was hit.
Here’s the script:
local tool = script.Parent
local db = false
local handle = tool:WaitForChild("Handle")
local animation = tool:WaitForChild("Animation")
local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local remote = tool:WaitForChild("DamageEvent")
local char = plr.Character or plr.CharacterAdded:Wait()
local plrhumanoid = char:WaitForChild("Humanoid")
tool.Equipped:Connect(function(mouse)
print("Equipped.")
tool.Activated:Connect(function()
mouse.Button1Down:Connect(function()
if not db then
db = true
local track = plrhumanoid:WaitForChild("Animator"):LoadAnimation(animation)
track:Play()
handle.Touched:Connect(function(hit)
if not hit then
return
end
local humanoid = hit.Parent:WaitForChild("Humanoid") or hit.Parent.Parent:WaitForChild("Humanoid")
local humanoidRootPart = hit.Parent:WaitForChild("HumanoidRootPart") or hit.Parent.Parent:WaitForChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
print("Humanoid found: " .. hit.Parent.Name)
local newAttachment = Instance.new("Attachment")
local newVectorForce = Instance.new("VectorForce")
newVectorForce.Attachment0 = newAttachment
newVectorForce.Parent = humanoidRootPart
newAttachment.Parent = humanoidRootPart
newVectorForce.Force = Vector3.new(0,0,-1000)
humanoid.Sit = true
remote:FireServer(humanoid,1)
task.wait(2.5)
humanoid.Sit = false
newVectorForce:Destroy()
newAttachment:Destroy()
elseif not humanoid and not humanoidRootPart or (not humanoid and humanoidRootPart) then
print("Humanoid was not found.")
end
task.wait(2.5)
db = false
end)
end
end)
end)
end)
You can try disconnecting from the Touched event when the player releases they Left Mouse Button.
tool.Activated:Connect(function()
local connection
mouse.Button1Down:Connect(function()
if not db then
db = true
local track = plrhumanoid:WaitForChild("Animator"):LoadAnimation(animation)
track:Play()
connection = handle.Touched:Connect(function(hit)
if not hit then
return
end
local humanoid = hit.Parent:WaitForChild("Humanoid") or hit.Parent.Parent:WaitForChild("Humanoid")
local humanoidRootPart = hit.Parent:WaitForChild("HumanoidRootPart") or hit.Parent.Parent:WaitForChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
print("Humanoid found: " .. hit.Parent.Name)
local newAttachment = Instance.new("Attachment")
local newVectorForce = Instance.new("VectorForce")
newVectorForce.Attachment0 = newAttachment
newVectorForce.Parent = humanoidRootPart
newAttachment.Parent = humanoidRootPart
newVectorForce.Force = Vector3.new(0,0,-1000)
humanoid.Sit = true
remote:FireServer(humanoid,1)
task.wait(2.5)
humanoid.Sit = false
newVectorForce:Destroy()
newAttachment:Destroy()
elseif not humanoid and not humanoidRootPart or (not humanoid and humanoidRootPart) then
print("Humanoid was not found.")
end
task.wait(2.5)
db = false
end)
end
end)
mouse.Button1Up:Connect(function()
if connection then
connection:Disconnect()
end
end)
end)
The variable connection is for tracking the touched event. Every time you connect to the Touched event of the handle you assign that connection to the variable connection. When the player releases their left mouse button, we check if something was assigned to the variable connection. If there is something, then we assume it’s a Touched event connection (because that’s the only thing you told your code to assign) and we disconnect it, so the Touched event for the handle won’t be listened to until you connect again.