I made an attack system for a sword i built, it plays an animation, does damage, creates a hitbox ect. But i wanted to make a clashing system for when two players attack at the same time. Similar to what Evolve Games does in every fighting game they make. So i started with an if statement, to attempt to check if any object named Hitbox touch each other, but i keep ending up with "attempt to index nil value" or "waiting for instance('Hitbox')". At this point i was getting frustrated and confused, so i came to ask scriptinghelpers.org for some help. Heres the script that i made
BasicAttack = game:GetService("ReplicatedStorage").RemoteFunctions:FindFirstChild("WeaponRemotes").ZangetsuRemotes["BasicAttack"]
baseAtk = math.random(20,30)
Assets = game:GetService("ReplicatedFirst"):WaitForChild("Assets")
debounce = {};
BasicAttack.OnServerEvent:connect(function(player)
player.Character.holder.Clashing.Value = true
----------------------------------------------------Variables
local Sword = player.Character.Tool.Blade
local clash = Instance.new('BodyVelocity')
local root = player.Character.HumanoidRootPart
local hitbox = Instance.new("Part", game.Workspace)
local name = player.Name
local folder = game:GetService("ReplicatedStorage"):FindFirstChild(name)
local Kendo = folder.Stats.Kendo
----------------------------------------------------Hitbox
hitbox.Name = "Hitbox"
hitbox.CFrame = Sword.CFrame
hitbox.CanCollide = false
hitbox.Size = Vector3.new(10,2,1)
hitbox.Transparency = 0.5
hitbox.BrickColor = BrickColor.new("Really red")
---------------------------------------------------Welding
local w1 = Instance.new("Weld", Sword)
w1.Part0 = Sword
w1.Part1 = hitbox
w1.C0 = CFrame.Angles(0,0,5)
w1.C1 = CFrame.new(0,-0.3,0)
--------------------------------------------------Damage
hitbox.Touched:connect(function(hit)
local dmg = math.floor(Kendo.Value/ 1.15 + baseAtk)
local enemyWeapon = hit.Parent:findFirstChild('Hitbox') ----searching for enemy hitbox
local opponent = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
if opponent and opponent ~= player.Character.Humanoid and opponent.Parent.holder.Clashing.Value == false then
if debounce[opponent] then return end;
debounce[opponent] = true;
opponent:TakeDamage(dmg)
------------------------------------------------------
-----------------[Text/Gui and debounce]---------------------------
local effect = Instance.new("BillboardGui", opponent.Parent.Head)
effect.Size = UDim2.new(1,0,1,0)
wait()
local text = Instance.new("TextBox")
text.Parent = effect
text.TextSize = 60
text.TextColor3 = Color3.new(255,255,255)
text.TextStrokeColor3 = Color3.new(95,0,0)
text.TextStrokeTransparency = 0
text.Font = "Cartoon"
text.BackgroundTransparency = 1
text.Size = UDim2.new(1,0,1,0)
text.Text = dmg
wait(0.5)
effect:destroy()
debounce[opponent] = nil;
-----------------------------------------------------------------Clashing system starts here
elseif enemyWeapon ~= hitbox and opponent.Parent.holder.Clashing.Value == true then
if opponent and opponent ~= player.Character.Humanoid and opponent.Parent.holder.Clashing.Value == false then
if debounce[opponent] then return end;
debounce[opponent] = true;
clash.Parent = root
clash.maxForce = Vector3.new(1e8,1e8,1e8)
clash.Velocity = root.CFrame.lookVector*-60
wait()
clash.Velocity = Vector3.new(0,0,0)
clash:remove()
debounce[opponent] = nil;
end
end
end)
wait(0.85)
hitbox:remove()
wait(0.5)
player.Character.holder.Clashing.Value = false
end)
