local Knife = script.Parent
local Player = game.Players.LocalPlayer
local Char = Player.Character
local Anim = script.Parent.Animation
local PlayAnimation = Char.Humanoid:LoadAnimation(Anim)
local HitBox = script.Parent.HitBox
local CanSwing = true
local CanDamage = true
local debounce = 1
Knife.Activated:Connect(function()
if CanSwing then
CanSwing = false
PlayAnimation:Play()
HitBox.Touched:Connect(function(p)
if p.Parent.Humanoid then
p.Parent.Humanoid:TakeDamage(20)
end
end)
wait(1)
CanSwing = true
end
end)
You could add a :WaitForChild()
1 Like
As mentioned earlier you need to add a waitforchild since the children may not be available to the local script at runtime, here is how you would do it (Also made some other fixes):
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait() --fixed here
local Knife = script.Parent
local Anim = Knife:WaitForChild("Animation")-- fixed here
local PlayAnimation = Char.Humanoid:LoadAnimation(Anim)
local HitBox = Knife:WaitForChild("HitBox") -- Fixed here
local CanSwing = true
local CanDamage = true
local debounce = 1
Knife.Activated:Connect(function()
if CanSwing then
CanSwing = false
PlayAnimation:Play()
HitBox.Touched:Connect(function(p)
if p.Parent.Humanoid then
p.Parent.Humanoid:TakeDamage(20)
end
end)
wait(1)
CanSwing = true
end
end)
If this code does not work, that means you do not actually have an AnimationObject within the knife. Also, you are dealing damage incorrectly, you need to do this on the server, and also your if statement logic is incorrect, and you need to do this:
HitBox.Touched:Connect(function(Hit)
local Humanoid = (Hit) and (Hit.Parent:FindFirstChild("Humanoid"))
if (Humanoid) and (Humanoid ~= Char:WaitForChild("Humanoid") then
Parent.Humanoid:TakeDamage(20)
end
end)
4 Likes