I’m trying to make a hammer that unanchors anchored part when hit. This is my script and it doesn’t work.
Attempt 1:
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local smash
remoteEvent.OnServerEvent:Connect(function(player, character, charged, magnitude)
local rootPart = character:WaitForChild("HumanoidRootPart")
if charged then
smash = head.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local hitCharacter = hit.Parent
local hitRootPart = hitCharacter:WaitForChild("HumanoidRootPart")
if humanoid then
local knockback = Instance.new("BodyVelocity")
hitRootPart.Anchored = false --here
knockback.Parent = hitRootPart
knockback.Velocity = (rootPart.CFrame.LookVector * magnitude) + (rootPart.CFrame.UpVector * magnitude)
knockback.MaxForce = knockback.MaxForce * magnitude
wait()
knockback:Destroy()
end
end)
wait(0.3)
smash:Disconnect()
end
end)
Attempt 2:
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local smash
remoteEvent.OnServerEvent:Connect(function(player, character, charged, magnitude)
local rootPart = character:WaitForChild("HumanoidRootPart")
if charged then
smash = head.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local hitCharacter = hit.Parent
local hitRootPart = hitCharacter:WaitForChild("HumanoidRootPart")
if humanoid then
if hitRootPart.Anchored == true then
hitRootPart.Anchored = false --here
end
local knockback = Instance.new("BodyVelocity")
knockback.Parent = hitRootPart
knockback.Velocity = (rootPart.CFrame.LookVector * magnitude) + (rootPart.CFrame.UpVector * magnitude)
knockback.MaxForce = knockback.MaxForce * magnitude
wait()
knockback:Destroy()
end
end)
wait(0.3)
smash:Disconnect()
end
end)
Attempt 3:
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local smash
remoteEvent.OnServerEvent:Connect(function(player, character, charged, magnitude)
local rootPart = character:WaitForChild("HumanoidRootPart")
if charged then
smash = head.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local hitCharacter = hit.Parent
local hitRootPart = hitCharacter:WaitForChild("HumanoidRootPart")
if hitRootPart.Anchored == true then
hitRootPart.Anchored = false --here
end
if humanoid then
local knockback = Instance.new("BodyVelocity")
knockback.Parent = hitRootPart
knockback.Velocity = (rootPart.CFrame.LookVector * magnitude) + (rootPart.CFrame.UpVector * magnitude)
knockback.MaxForce = knockback.MaxForce * magnitude
wait()
knockback:Destroy()
end
end)
wait(0.3)
smash:Disconnect()
end
end)