I’m trying to make a knockback hammer that anchors (freezes) you for a certain amount of time when you swing it. But when other players hit you with the hammer, you should get unanchored, assuming you’re anchored. But the issue of my script is, it does nothing.
Client: (I don’t think the problem is in the client tho but here it is…)
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local remoteEvent = tool.RemoteEvent
local animator = humanoid:WaitForChild("Animator")
local charge = script.Charge
local smash = script.Smash
local chargeTrack = animator:LoadAnimation(charge)
local smashTrack = animator:LoadAnimation(smash)
local cooldown = false
local charged = false
local hanging = false
local charging = false
local magnitude = 100
tool.Activated:Connect(function()
if not cooldown and not hanging then
cooldown = true
if not charged and not charging then
charging = true
smashTrack:Stop()
chargeTrack:Play()
delay(chargeTrack.Length - 0.02999, function()
chargeTrack:AdjustSpeed(0)
end)
charged = true
for i = 50, 100 do
if charging then
wait()
magnitude = i
end
end
end
wait(1)
cooldown = false
end
end)
tool.Deactivated:Connect(function()
chargeTrack:Stop()
if charged then
charging = false
smashTrack:Play()
remoteEvent:FireServer(character, charged, magnitude)
delay(smashTrack.Length - 0.02999, function()
smashTrack:AdjustSpeed(0)
rootPart.Anchored = true
hanging = true
wait(1)
smashTrack:Stop()
rootPart.Anchored = false
hanging = false
end)
end
charged = false
end)
tool.Unequipped:Connect(function()
chargeTrack:Stop()
smashTrack:Stop()
cooldown = false
charged = false
hanging = false
charging = false
end)
Server:
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:FindFirstChild("HumanoidRootPart")
if charged then
smash = head.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local hitCharacter = hit.Parent
local hitRootPart = hitCharacter:FindFirstChild("HumanoidRootPart")
if humanoid then
local knockback = Instance.new("BodyVelocity")
hitRootPart.Anchored = false --here fella!
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)