i have a script that plays an animation and deals damage when the right mouse button is clicked, it works perfectly. but the problem occurs when im trying to look around and i end up attacking
heres my script, i think i could use delay or ticks to find out how long ago the key was pressed (if that makes sense
local script:
local player = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")
local M2Anim = script:WaitForChild("M2Anim")
local CD = 4.5
local debounce = false
local M2Remote = game.ReplicatedStorage.M2Remote.M2Fire
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local gaurdingAnimation = hum:LoadAnimation(M2Anim)
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, Typing)
if not Typing then
if input.UserInputType == Enum.UserInputType.MouseButton2 and debounce == false then
if char:FindFirstChild("Stun") == nil and char:FindFirstChild("eStun") == nil then
debounce = true
M2Remote:FireServer()
local sfx = workspace.Audio.AbilityAudio.Audio.M2:Clone()
sfx.Parent = player.Character.Torso
sfx:Play()
game.Debris:AddItem(sfx,1)
hum.WalkSpeed = 3.5
gaurdingAnimation:Play()
delay(1,function()
hum.WalkSpeed = 16
end)
wait(CD)
debounce = false
end
end
end
end)
server script:
local m2Event = game.ReplicatedStorage.M2Remote.M2Fire
m2Event.OnServerEvent:Connect(function(plr)
local char = plr.Character
local hb = char:FindFirstChild("Torso")
local root = char:FindFirstChild("HumanoidRootPart")
local ws = game.Workspace.PlayersAndMobs
for i,v in pairs(ws:GetChildren()) do -- look through all the workspace
if v:FindFirstChild("Humanoid") and v:FindFirstChild("Head") and v.Name ~= plr.Name then --if the thing we are looking in has a humanoid and a head and its name isnt the players name then
local dist = (v.Head.Position - hb.Position).magnitude --find the distance between us and the ting
if dist < 5 then -- if its less than 35 studs then
v.Humanoid:TakeDamage(9) -- take 30 damage
script.Parent.HitSound:Play()
local fxcln = script.fx:Clone()
fxcln.Parent = v.Head.Parent.HumanoidRootPart
fxcln.CFrame = v.Head.Parent.HumanoidRootPart.CFrame
game.Debris:AddItem(fxcln,.3)
local bv = Instance.new("BodyVelocity",v.HumanoidRootPart) --make the knockback
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge) -- maximum force
bv.Velocity = root.CFrame.lookVector * 45 + Vector3.new(0,7,10) -- the force
game.Debris:AddItem(bv,.3) --it will last 0.5 seconds
if char:FindFirstChild("Blocking") == true then char:FindFirstChild("Blocking").Value = false
workspace.Audio.AbilityAudio.Audio.BlockBreak:Play()
end
end
end
end
end)