I’m making a fighting game and i tried making a script that lets the player dash and give them the option to do an attack. They can perform the regular dash attack or the ‘electric’, more powerful version of the attack if they click almost immediately after dashing. It’s not complete yet, but i want to know if this script can be further optimised / improved. The attacks themselves will be implemented and handled with server scripts.
‘MoveEvent’ is a remote event parented to the LocalScript that signals the server to either turn on or turn off the player’s ‘IsDashing’ value from their CoreValues.
Sorry if the code is a mess to read right now.
local uis = game:GetService("UserInputService")
local anims = game:GetService("ReplicatedStorage"):WaitForChild("Animations")
local player = game.Players.LocalPlayer
local char = player.Character
local charcore = char:WaitForChild("CoreValues")
local hum = char:WaitForChild("Humanoid")
local event = char:WaitForChild("Connection")
local animlist = anims:WaitForChild("IronFist")
local lastpressW = os.clock()
local lastpressS = os.clock()
local dpwindow = 0.3
local candash = true
local dashcd = 0.4
local bdashcd = 0.9
local earlycandashtime = 0.3
local dashedtick = nil
local function dash()
print("d")
candash = false
event:FireServer("Dash", true)
local velocity = Instance.new("LinearVelocity")
velocity.Parent = char.HumanoidRootPart
velocity.MaxForce = 17000
velocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
velocity.VectorVelocity = Vector3.new(0, 0, -45)
velocity.Attachment0 = char.HumanoidRootPart.RootRigAttachment
hum:LoadAnimation(animlist.Dash):Play()
debris.AddItem(velocity, 0.25)
dashedtick = os.clock()
task.wait(earlycandashtime)
candash = true
task.wait(dashcd - earlycandashtime)
if candash == true then
event:FireServer("Dash", false)
end
end
local function backdash()
print("bd")
candash = false
event:FireServer("Backdash", true)
local velocity = Instance.new("LinearVelocity")
velocity.Parent = char.HumanoidRootPart
velocity.MaxForce = 16000
velocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
velocity.VectorVelocity = Vector3.new(0, 0, 45)
velocity.Attachment0 = char.HumanoidRootPart.RootRigAttachment
char.Humanoid:LoadAnimation(animlist.Backdash):Play()
debris.AddItem(velocity, 0.15)
task.wait(earlycandashtime)
candash = true
task.wait(bdashcd - earlycandashtime)
if candash == true then
event:FireServer("Backdash", false)
end
end
uis.InputBegan:Connect(function(input, gp)
if not gp then
if input.KeyCode == Enum.KeyCode.W then
local curr = os.clock()
print((curr - lastpressW))
if (curr - lastpressW) <= dpwindow and candash == true and charcore.Actionable.Value == true and hum.FloorMaterial ~= Enum.Material.Air then
dash()
end
lastpressW = curr
end
if input.KeyCode == Enum.KeyCode.S then
local curr = os.clock()
print((curr - lastpressS))
if (curr - lastpressS) <= dpwindow and candash == true and charcore.Actionable.Value == true and hum.FloorMaterial ~= Enum.Material.Air then
backdash()
end
lastpressS = curr
end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if charcore.IsDashing.Value == true and charcore.Actionable.Value == true then
if dashedtick ~= nil then
local clicked = os.clock()
print(clicked - dashedtick)
if (clicked - dashedtick) <= 0.05 then
print("electric")
else
print("regular")
end
end
end
end
end
end)