Sword Mouse Click Damage Debounce Issues [?]
Asked by
6 years ago Edited 6 years ago
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:
02 | local plr = game.Players.LocalPlayer |
03 | local Char = plr.Character or plr.CharacterAdded:Wait() |
04 | local Humanoid = Char.Humanoid |
05 | local Mouse = plr:GetMouse() |
07 | local EquippedItems = plr:WaitForChild( 'EquippedItems' ) |
08 | local Weapons = plr:WaitForChild( 'Weapons' ) |
09 | local PlayerStatus = plr:WaitForChild( 'PlayerStatus' ) |
12 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
13 | local UIS = game:GetService( "UserInputService" ) |
16 | local EW = ReplicatedStorage.Weapons [ EquippedItems.CurrentWeapon.Value ] |
17 | local Equipped = PlayerStatus.WeaponEquipped |
21 | DamageEvent = ReplicatedStorage.RemoteEvents.DamageEvent; |
25 | local Animation = Instance.new( 'Animation' ) |
27 | local SwordAnimations = { |
34 | local ItemInformation = require(ReplicatedStorage.ItemInformation) |
37 | Mouse.Button 1 Down:Connect( function () |
38 | if Equipped.Value and Debounce and Char then |
40 | if ItemInformation [ EquippedItems.CurrentWeapon.Value ] .Type = = 'Sword' then |
41 | local HitDebounce = true |
42 | Animation.AnimationId = SwordAnimations [ Count ] |
43 | local Load = Humanoid:LoadAnimation(Animation) |
46 | HitBoxTouchCon = Char.UpperTorso [ EquippedItems.CurrentWeapon.Value ] .HitBox.Touched:Connect( function (H) |
47 | if Load.IsPlaying and HitDebounce and H.Parent:FindFirstChild( 'Humanoid' ) ~ = nil then |
49 | local Enemy = H.Parent.Humanoid |
50 | RE.DamageEvent:FireServer(ItemInformation [ EquippedItems.CurrentWeapon.Value ] [ "Damage" ] , Enemy, EquippedItems.CurrentWeapon.Value) |
54 | if Count > #SwordAnimations then |
This is only part of my code. note that this is a LocalScript
Server Script:
2 | RE.DamageEvent.OnServerEvent:Connect( function (plr, Dmg, Enemy, Wep) |
3 | if Dmg = = ItemInformation [ Wep ] .Damage and SerDmgD then |
My other part is a UserInputService and welds the Sword to the player.
I appreciate any feedback that will help Solve the problem.