I’ve got a block system for my sword in-game and the unblock doesn’t work the first time. But the second time the player blocks, they will actually unblock. The first time they block, the player becomes immortal and doesn’t take damage.
ill put the code for the sword below:
local remote = game.ReplicatedStorage.Remotes.Combat
remote.OnServerEvent:Connect(function(p,data)
if data.action == "slash" then
for i,player in pairs(game.Players:GetChildren()) do
if player ~= p then
remote:FireClient(player, data)
end
end
elseif data.action == "hit" then
remote:FireAllClients( data)
for i,v in pairs(data.hb) do
if v:FindFirstChild("Block") then
if v.Block.Value == true then
local parryAnim = Instance.new("Animation")
parryAnim.AnimationId = data.parry
v.Humanoid:LoadAnimation(parryAnim):Play()
parryAnim:Destroy()
else
local hitAnim = Instance.new("Animation")
hitAnim.AnimationId = data.hit
v.Humanoid:LoadAnimation(hitAnim):Play()
hitAnim:Destroy()
v.Humanoid:TakeDamage(10)
end
else
local hitAnim = Instance.new("Animation")
hitAnim.AnimationId = data.hit
v.Humanoid:LoadAnimation(hitAnim):Play()
hitAnim:Destroy()
v.Humanoid:TakeDamage(10)
end
local bv = Instance.new("BodyVelocity", v.PrimaryPart)
bv.MaxForce = Vector3.new(99999,0,99999)
bv.Name = "v"
bv.P = 10
game.Debris:AddItem(bv,.2)
if data.combo == 5 then
bv.Velocity = data.c.PrimaryPart.CFrame.LookVector * 35
else
bv.Velocity = data.c.PrimaryPart.CFrame.LookVector * 15
end
local bv = Instance.new("BodyVelocity",data.c.PrimaryPart)
bv.MaxForce = Vector3.new(99999,0,99999)
bv.Name = "v"
bv.P = 10
game.Debris:AddItem(bv,.2)
if data.combo == 5 then
bv.Velocity = data.c.PrimaryPart.CFrame.LookVector * 25
else
bv.Velocity = data.c.PrimaryPart.CFrame.LookVector * 10
end
local stun = Instance.new("BoolValue", v)
stun.Name = "Stun"
stun.Value = false
game.Debris:AddItem(stun,1)
end
elseif data.action == "block" then
if data.char:FindFirstChild("Block") then
data.char.Block.Value = true
else
local block = Instance.new("BoolValue", data.char)
block.Value = true
block.Name = "Block"
end
elseif data.action == "unblock" then
if data.char:FindFirstChild("Block") then
data.char.Block.Value = false
print("Unblocked")
end
end
end)
local swingAnims = {
'rbxassetid://10799628961', --1
'rbxassetid://10799639636', --2
'rbxassetid://10805184969', --3
'rbxassetid://10805270660', --4
'rbxassetid://10799639636', --5
}
local textures = {
'rbxassetid://8821193347',
'rbxassetid://8821230983',
'rbxassetid://8821246947',
'rbxassetid://8821254467',
'rbxassetid://8821272181',
'rbxassetid://8821280832',
'rbxassetid://8821300395',
'rbxassetid://8821311218',
'rbxassetid://8896641723',
}
local blockAnims = {
'rbxassetid://10875156928', -- block
'rbxassetid://10822445779', -- parry
'rbxassetid://10817949810', -- hit
}
local t = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local resources = rs.Combat.Sword
local fx = resources.Fx
local remote = rs.Remotes.Combat
local p = game.Players.LocalPlayer
local c = script.Parent
local hrp = c.HumanoidRootPart
local combo = 1
local lasthit = 0
local cd = false
local anim = script:WaitForChild("Animation")
local blocking = false
local UserInputService = game:GetService("UserInputService") -- get userinputservice
local function setUp(char)
local handle = fx.Handle:Clone()
handle.Parent = char
local m6d = Instance.new("Motor6D", handle)
m6d.Part0 = char.RightHand
m6d.Part1 = handle
m6d.C1 = CFrame.new(0,.125,0)
local model = fx.BasicKatana:Clone()
model.Parent = workspace.Fx
local weld = Instance.new("Weld", model)
weld.Part0 = handle
weld.Part1 = model
weld.C1 = CFrame.new(0,0,0) * CFrame.Angles(math.rad(90),0,0)
end
for i,v in pairs(game.Players:GetChildren() ) do
repeat wait() until v.Character
setUp(v.Character)
v.CharacterAdded:Connect(function(char)
char:WaitForChild("RightHand")
setUp(char)
end)
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
char:WaitForChild("RightHand")
setUp(char)
end)
end)
local function slash(char, comboN)
local m = fx.Slash:Clone()
local hrp = char.HumanoidRootPart
if comboN == 1 then
wait(.2)
m.CFrame = hrp.CFrame * CFrame.new(2.5,0,-3) * CFrame.Angles(0,math.rad(180),math.rad(-275))
t:Create(m, TweenInfo.new(.5), {CFrame = m.CFrame * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
elseif comboN == 2 then
wait(.2)
m.CFrame = hrp.CFrame * CFrame.new(0,0,-3) * CFrame.Angles(0,math.rad(30),math.rad(0))
t:Create(m, TweenInfo.new(.5), {CFrame = m.CFrame * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
elseif comboN == 3 then
wait(.2)
m.CFrame = hrp.CFrame * CFrame.new(0,0,-3) * CFrame.Angles(0,math.rad(0),math.rad(180))
t:Create(m, TweenInfo.new(.5), {CFrame = m.CFrame * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
elseif comboN == 4 then
wait(.2)
m.CFrame = hrp.CFrame * CFrame.new(0,0,-3) * CFrame.Angles(0,math.rad(30),math.rad(50))
t:Create(m, TweenInfo.new(.5), {CFrame = m.CFrame * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
elseif comboN == 5 then
wait(.3)
m.CFrame = hrp.CFrame * CFrame.new(2.5,0,-3) * CFrame.Angles(0,math.rad(180),math.rad(-275))
t:Create(m, TweenInfo.new(.5), {CFrame = m.CFrame * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
m.CFrame = hrp.CFrame * CFrame.new(0,0,-6) * CFrame.Angles(0,math.rad(30),math.rad(0))
t:Create(m, TweenInfo.new(.5), {CFrame = hrp.CFrame * CFrame.new(0,0,-10) * CFrame.Angles(0,math.rad(-100),0), Transparency = 1}):Play()
end
t:Create(m.PointLight, TweenInfo.new(.4), {Brightness = 0}):Play()
local count = 1
local connection
connection = game["Run Service"].RenderStepped:Connect(function()
m.Mesh.TextureId = textures[count]
count = count + 1
if count > #textures then
count = 1
connection:Disconnect()
end
end)
m.Parent = workspace.Fx
if char == c then
local hb = Instance.new("Part", workspace.Fx)
hb.Size = Vector3.new(10,3,7)
hb.CFrame = hrp.CFrame * CFrame.new(0,0,-5)
hb.Anchored = true
hb.CanCollide = false
hb.Transparency = .6
hb.Name = "hb"
hb.Material = Enum.Material.ForceField
hb.CanQuery = false
local con
con = hb.Touched:Connect(function()
con:Disconnect()
end)
local hits = {}
for i,v in pairs(hb:GetTouchingParts()) do
if v:IsDescendantOf(char) == false then
if v.Parent:FindFirstChildWhichIsA("Humanoid") and table.find(hits, v.Parent) == nil then
table.insert(hits, v.Parent)
end
end
end
if #hits == 0 then
local bv = Instance.new("BodyVelocity",char.PrimaryPart)
bv.MaxForce = Vector3.new(99999,0,99999)
bv.Name = "v"
bv.P = 10
game.Debris:AddItem(bv,.2)
if comboN == 5 then
bv.Velocity = char.PrimaryPart.CFrame.LookVector * 40
else
bv.Velocity = char.PrimaryPart.CFrame.LookVector * 25
end
end
local data = {hb = hits, action = "hit", combo = comboN, c = char, parry = blockAnims[2], hit = blockAnims[3]}
remote:FireServer(data)
hb:Destroy()
end
end
uis.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and c:FindFirstChild("Stun") == nil then
c.Humanoid.WalkSpeed = 2
c.Humanoid.JumpHeight = 1
if tick() - lasthit > 1 then
combo = 1
end
lasthit = tick()
cd = true
anim.AnimationId = swingAnims[combo]
local load = c.Humanoid:LoadAnimation(anim):Play()
local data = {char = c, combo, action = "slash"}
remote:FireServer(data)
slash(c, combo)
if combo == 5 then
combo = 1
else
combo = combo + 1
end
wait(.1)
cd = false
c.Humanoid.WalkSpeed = 16
c.Humanoid.JumpHeight = 7.2
elseif input.UserInputType == Enum.UserInputType.MouseButton2 and cd == false and c:FindFirstChild("Stun") == nil then
anim.AnimationId = blockAnims[1]
local load = c.Humanoid:LoadAnimation(anim)
load:Play()
blocking = true
local data = {char = c, action = "block"}
remote:FireServer(data)
repeat
wait()
until blocking == false
load:Stop()
end
end)
uis.InputBegan:Connect(function(input, gpe)
if input.UserInputType == Enum.UserInputType.MouseButton2 and blocking then
blocking = false
local data = {char = c, action = "unblock"}
wait(0.8)
remote:FireServer(data)
end
end)
remote.OnClientEvent:Connect(function(data)
if data.action == "slash" then
slash(data.char, data.comboN)
elseif data.action == "hit" then
for i,v in pairs(data.hb) do
if v:FindFirstChild("Block") then
if v.Block.Value == true then
local hit = fx.Block:Clone()
hit.Anchored = false
local weld = Instance.new("Weld", hit)
weld.Part0 = v.PrimaryPart
weld.Part1 = hit
hit.Position = v.PrimaryPart.Position
hit.Attachment.Left:Emit(15)
hit.Attachment.Right:Emit(15)
hit.Attachment.OtherLines:Emit(15)
hit.Parent = workspace.Fx
t:Create(hit.Attachment.light, TweenInfo.new(.4), {Brightness = 0}):Play()
game.Debris:AddItem(hit,1)
else
local hit = fx.Hit:Clone()
hit.Anchored = false
local weld = Instance.new("Weld", hit)
weld.Part0 = v.PrimaryPart
weld.Part1 = hit
weld.C1 = weld.C1 * CFrame.Angles(0,0,math.rad(math.random(-300,300)))
hit.Position = v.PrimaryPart.Position
hit.Attachment.Left:Emit(15)
hit.Attachment.OtherLines:Emit(15)
hit.Attachment.Right:Emit(15)
hit.Parent = workspace.Fx
t:Create(hit.Attachment.light, TweenInfo.new(.4), {Brightness = 0}):Play()
game.Debris:AddItem(hit,1)
end
else
local hit = fx.Hit:Clone()
hit.Anchored = false
local weld = Instance.new("Weld", hit)
weld.Part0 = v.PrimaryPart
weld.Part1 = hit
weld.C1 = weld.C1 * CFrame.Angles(0,0,math.rad(math.random(-300,300)))
hit.Position = v.PrimaryPart.Position
hit.Attachment.Left:Emit(15)
hit.Attachment.OtherLines:Emit(15)
hit.Attachment.Right:Emit(15)
hit.Parent = workspace.Fx
t:Create(hit.Attachment.light, TweenInfo.new(.4), {Brightness = 0}):Play()
game.Debris:AddItem(hit,1)
end
end
end
end)
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://5917820543"
if not sound.IsLoaded then -- wait for sound to load to prevent errors
sound.Loaded:Wait()
end
UserInputService.InputBegan:Connect(function(input) -- when player presses a key
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and combo == 1 then
sound:Play() -- play the sound
print("sound played")
end
end)
local sound1 = Instance.new("Sound", game.Workspace)
sound1.SoundId = "rbxassetid://5917819729"
if not sound1.IsLoaded then -- wait for sound to load to prevent errors
sound1.Loaded:Wait()
end
UserInputService.InputBegan:Connect(function(input) -- when player presses a key
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and combo == 2 then
sound1:Play() -- play the sound
print("sound played")
end
end)
local sound2 = Instance.new("Sound", game.Workspace)
sound2.SoundId = "rbxassetid://5917820121"
if not sound2.IsLoaded then -- wait for sound to load to prevent errors
sound2.Loaded:Wait()
end
UserInputService.InputBegan:Connect(function(input) -- when player presses a key
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and combo == 3 then
sound2:Play() -- play the sound
print("sound played")
end
end)
local sound3 = Instance.new("Sound", game.Workspace)
sound3.SoundId = "rbxassetid://5917820543"
if not sound3.IsLoaded then -- wait for sound to load to prevent errors
sound3.Loaded:Wait()
end
UserInputService.InputBegan:Connect(function(input) -- when player presses a key
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and combo == 4 then
sound3:Play() -- play the sound
print("sound played")
end
end)
local sound4 = Instance.new("Sound", game.Workspace)
sound4.SoundId = "rbxassetid://9119746592"
if not sound4.IsLoaded then -- wait for sound to load to prevent errors
sound4.Loaded:Wait()
end
UserInputService.InputBegan:Connect(function(input) -- when player presses a key
if input.UserInputType == Enum.UserInputType.MouseButton1 and cd == false and combo == 5 then
sound4:Play() -- play the sound
print("sound played")
end
end)
I put the full code in here because there might have been a small mistake I have made, and haven’t realised.