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.
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.
--// 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.