I’m trying to make a hammer that has to be charged before being able to be swung. And by swinging, it’ll do knockback to hit. But the problem is, it still does knockback even without swinging. (Just by touching the dummies)
This is what the scripts looks like right now…
Client:
local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local rootPart = character:WaitForChild("HumanoidRootPart")
local remoteEvent = tool.RemoteEvent
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 canKnockback = false
tool.Activated:Connect(function()
if not cooldown and not hanging then
cooldown = true
if not charged then
smashTrack:Stop()
chargeTrack:Play()
delay(chargeTrack.Length - 0.02999, function()
chargeTrack:AdjustSpeed(0)
end)
charged = true
end
wait(1)
cooldown = false
end
end)
tool.Deactivated:Connect(function()
chargeTrack:Stop()
if charged then
remoteEvent:FireServer(charged)
smashTrack:Play()
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)
Server:
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local function knockback()
end
remoteEvent.OnServerEvent:Connect(function(player, command)
if command then
head.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback") then
if not script.Parent:FindFirstChild(hit.Parent.Name) then
local debris = game:GetService("Debris")
local tag = Instance.new("IntValue")
tag.Parent = script.Parent
tag.Name = hit.Parent.Name
debris:AddItem(tag, 1)
local Intensity = 1000
local Knockback = Instance.new("BodyVelocity")
Knockback.Parent = hit.Parent.HumanoidRootPart
Knockback.Velocity = head.CFrame.LookVector * Intensity
Knockback.MaxForce = Knockback.MaxForce * Intensity
debris:AddItem(Knockback, 0.2)
end
end
end)
end
end)
If you can help, PLEASE do. I badly need help. I’ve been trying and trying for so long and it’s becoming painfully stressful. Thanks! <3
All you’d just really need to do is pass the tuple argument in this parameter I believe, but which one would you really want though from your Bools? I’m gonna guess the charged one
remoteEvent.OnServerEvent:Connect(function(player, command)
print(command)
if command == true then
head.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback") then
if not script.Parent:FindFirstChild(hit.Parent.Name) then
local debris = game:GetService("Debris")
local tag = Instance.new("IntValue")
tag.Parent = script.Parent
tag.Name = hit.Parent.Name
debris:AddItem(tag, 1)
local Intensity = 1000
local Knockback = Instance.new("BodyVelocity")
Knockback.Parent = hit.Parent.HumanoidRootPart
Knockback.Velocity = head.CFrame.LookVector * Intensity
Knockback.MaxForce = Knockback.MaxForce * Intensity
debris:AddItem(Knockback, 0.2)
end
end
end)
end
end)
I would recommend not creating the connection to the .Touched event within the RemoteEvent. Create it outside of the RemoteEvent, and have the RemoteEvent set a variable to true. Then from within the .Touched event check to see if that variable is set to true, if it is, then apply knockback and set it back to false. Let me know if this fixes it for you.
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local function knockback()
head.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback") then
if not script.Parent:FindFirstChild(hit.Parent.Name) then
local debris = game:GetService("Debris")
local tag = Instance.new("IntValue")
tag.Parent = script.Parent
tag.Name = hit.Parent.Name
debris:AddItem(tag, 1)
local Intensity = 1000
local Knockback = Instance.new("BodyVelocity")
Knockback.Parent = hit.Parent.HumanoidRootPart
Knockback.Velocity = head.CFrame.LookVector * Intensity
Knockback.MaxForce = Knockback.MaxForce * Intensity
debris:AddItem(Knockback, 0.2)
end
end
end)
end
remoteEvent.OnServerEvent:Connect(function(player, command)
print(command)
if command then
knockback()
end
end)
local tool = script.Parent
local head = tool.Handle
local remoteEvent = tool.RemoteEvent
local knockback = false
head.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback") then
if not script.Parent:FindFirstChild(hit.Parent.Name) then
if knockback == true then
local debris = game:GetService("Debris")
local tag = Instance.new("IntValue")
tag.Parent = script.Parent
tag.Name = hit.Parent.Name
debris:AddItem(tag, 1)
local Intensity = 1000
local Knockback = Instance.new("BodyVelocity")
Knockback.Parent = hit.Parent.HumanoidRootPart
Knockback.Velocity = head.CFrame.LookVector * Intensity
Knockback.MaxForce = Knockback.MaxForce * Intensity
debris:AddItem(Knockback, 0.2)
knockback = false
end
end
end
end)
remoteEvent.OnServerEvent:Connect(function(player, command)
print(command)
if command then
knockback = true
end
end)
I got so much closer to what I want now, thanks to you!!! But there’s still a problem…
So, when I swing the hammer in air, (No dummies were hit) and then I go and touch a dummy without swinging, it does knockback. Please help if you still can, thanks again!