#Goal:# My goal is to simply create a Sword that will play an animation and deal damage on click. I want the damage to ONLY deal damage when the animation is playing and touched the enemy.
#Problem:# Everything works fine except the damaging part. So say an enemy is 100 studs away if I click once and then run up to the enemy and the sword touches the enemy without me clicking again it will deal damage. the system basically stores it as this player has clicked so I can now deal damage which isn't what I want. I want the player to click, play the animation and during the animation playing if sword touches enemy it deals damage.
#Code:#
--// Normal Vars \\--
local plr = game.Players.LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = Char.Humanoid
local Mouse = plr:GetMouse()
local EquippedItems = plr:WaitForChild('EquippedItems')
local Weapons = plr:WaitForChild('Weapons')
local PlayerStatus = plr:WaitForChild('PlayerStatus')
--// Services \\--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
--// Settings \\--
local EW = ReplicatedStorage.Weapons[EquippedItems.CurrentWeapon.Value]
local Equipped = PlayerStatus.WeaponEquipped
--// RemoteEvents \\--
local RE = {
DamageEvent = ReplicatedStorage.RemoteEvents.DamageEvent;
}
--// Animation Ids \\--
local Animation = Instance.new('Animation')
local SwordAnimations = {
'rbxassetid://2042283613',
'rbxassetid://2042413532',
}
local Count = 1
local ItemInformation = require(ReplicatedStorage.ItemInformation)
local Debounce = true
Mouse.Button1Down:Connect(function()
if Equipped.Value and Debounce and Char then
Debounce = false
if ItemInformation[EquippedItems.CurrentWeapon.Value].Type == 'Sword' then
local HitDebounce = true
Animation.AnimationId = SwordAnimations[Count]
local Load = Humanoid:LoadAnimation(Animation)
Load:Play()
local HitBoxTouchCon
HitBoxTouchCon = Char.UpperTorso[EquippedItems.CurrentWeapon.Value].HitBox.Touched:Connect(function(H)
if Load.IsPlaying and HitDebounce and H.Parent:FindFirstChild('Humanoid') ~= nil then
HitDebounce = false
local Enemy = H.Parent.Humanoid
RE.DamageEvent:FireServer(ItemInformation[EquippedItems.CurrentWeapon.Value]["Damage"], Enemy, EquippedItems.CurrentWeapon.Value)
end
end)
Count = Count + 1
if Count > #SwordAnimations then
Count = 1
end
wait(0.7)
HitDebounce = true
Debounce = true
end
end
end)
This is only part of my code. note that this is a LocalScript
Server Script:
local SerDmgD = true
RE.DamageEvent.OnServerEvent:Connect(function(plr, Dmg, Enemy, Wep)
if Dmg == ItemInformation[Wep].Damage and SerDmgD then
SerDmgD = false
Enemy:TakeDamage(Dmg)
wait(0.6)
SerDmgD = true
end
end)
My other part is a UserInputService and welds the Sword to the player.
I appreciate any feedback that will help Solve the problem.

3 comments
How else would I manage Touch, and how else would I said ~= nil
Handle the touching part in the serverscript as incapaz said and make another script which will damage another player on contact and make it disabled by default, then everytime the player clicks you enable the damage script for the duration of the animation (i.e if the animation is 2 seconds long enable it for 2 seconds and then disable it so the touch event doesn't get fired afterwards.)
Try putting the Damage code in a KeyframeReached event and assign a Keyframe name in the editor. Here is some more information. https://wiki.roblox.com/index.php?title=API:Class/AnimationTrack/KeyframeReached