I’m currently making a fighting game, and i’m trying to make a combat system, for some reason, the stun system is completely bugging out and idk why, i can’t even explain, here’s what is happening:
Not only that, sometimes it even makes me attack
fast, that i can kill my friend in seconds!
Here’s my code, any help would be appreciated! (also it’s not a problem with the hitbox before many people ask to me to CHANGE THE MODULE COMPLETELY)
local CombatEvent = game.ReplicatedStorage:WaitForChild("Events").CombatEvent
local Modules = game.ReplicatedStorage:WaitForChild("Modules")
local Animations = game.ReplicatedStorage.Animations
local Effects = game.ReplicatedStorage.Effects
local SoundsFolder = game.ReplicatedStorage.Sounds
local SwingSound = SoundsFolder.Swing
local HitSound = SoundsFolder.Hit
--local DB = false
local Combo = 0
game.Players.PlayerAdded:Connect(function(Player)
local Stunned = Instance.new("BoolValue", Player)
Stunned.Name = "Stunned"
Stunned.Value = false
end)
function Stun(HitPlayer)
task.delay(1, function()
HitPlayer.Stunned.Value = false
end)
end
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
local HitboxModule = require(Modules.MuchachoHitbox)
CombatEvent.OnServerEvent:Connect(function(Player)
if Player.Stunned.Value == false then
local NewHitbox = HitboxModule.CreateHitbox()
local SlashClone = Effects.Slash:Clone()
local Character = Player.Character
SlashClone.Parent = Character.HumanoidRootPart.RootAttachment
SwingSound:Play()
--DB = true
Params.FilterDescendantsInstances = {Character}
NewHitbox.Visualizer = false
NewHitbox.Size = Vector3.new(6, 6, 6)
NewHitbox.CFrame = Character.HumanoidRootPart
NewHitbox.Offset = CFrame.new(0, 0, -3)
NewHitbox.OverlapParams = Params
Player.Stunned.Value = true
Combo += 1
if Combo >= 3 then
Combo = 1
end
local AnimTrack = Character.Humanoid:LoadAnimation(Animations:FindFirstChild("Slash"..Combo))
AnimTrack:Play()
NewHitbox.Touched:Connect(function(hit, hum)
local HitClone = Effects.Hit:Clone()
local HitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
local StunFunction = nil
if StunFunction ~= nil then
task.cancel(StunFunction)
end
if HitPlayer then
HitPlayer.Stunned.Value = true
end
HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
hum:TakeDamage(10)
HitSound:Play()
hum:LoadAnimation(Animations:FindFirstChild("Hit"..Combo)):Play()
task.delay(0.25, function()
HitClone:Destroy()
end)
if HitPlayer then
task.delay(1, function()
StunFunction = task.spawn(Stun, HitPlayer)
end)
end
end)
NewHitbox:Start()
task.delay(0.27, function()
if Player.Stunned.Value == true then
SlashClone:Destroy()
Player.Stunned.Value = false
NewHitbox:Destroy()
end
end)
end
end)
I didn’t fully read into you’re code, but from a glance it seems to be an issue regarding you not disconnecting the Touched function along with task.delay.
If you don’t disconnect the Touched function it will continue to damage whoever touches the hitbox part even after the player is done swinging
local connection = nil
connection = NewHitbox.Touched:Connect(function(hit, hum)
end)
task.wait(1)
connection:Disconnect() --Make sure no one gets damaged after 1 second
The use of task.delay may also be causing some issues because they are continuing into the next attack.
Example:
--player swings (task.delay 1 second)
--player swings again 0.5 seconds later
--the 1 second task.delay now takes effect, but on a different swing
Again, I could be wrong about the task.delay causing issues but make sure you account for that on ALL of your task.delays.
Nevermind, i had to ask AI to help me to solve it, but it’s actually better now. Thanks for trying to help tho, also, for anyone curious, here’s the new script:
local CombatEvent = game.ReplicatedStorage:WaitForChild("Events").CombatEvent
local Modules = game.ReplicatedStorage:WaitForChild("Modules")
local Animations = game.ReplicatedStorage.Animations
local Effects = game.ReplicatedStorage.Effects
local SoundsFolder = game.ReplicatedStorage.Sounds
local SwingSound = SoundsFolder.Swing
local HitSound = SoundsFolder.Hit
local Combo = nil
--local DB = false
game.Players.PlayerAdded:Connect(function(Player)
local Stunned = Instance.new("BoolValue", Player)
Combo = Instance.new("IntValue", Player)
Stunned.Name = "Stunned"
Combo.Name = "Combo"
Combo.Value = 0
Stunned.Value = false
end)
--function Stun(HitPlayer)
-- task.delay(1, function()
-- HitPlayer.Stunned.Value = false
-- end)
--end
local StunCooldown = {} -- Um dicionário para armazenar o tempo de espera de cada jogador
function Stun(HitPlayer)
if HitPlayer.Stunned.Value == false then
HitPlayer.Stunned.Value = true
StunCooldown[HitPlayer.UserId] = 1 -- Define o cooldown para 1 segundo
-- ... (resto do código para aplicar o stun)
task.delay(0.3, function()
HitPlayer.Stunned.Value = false
StunCooldown[HitPlayer.UserId] = nil -- Remove o cooldown
end)
end
end
local Params = OverlapParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
local HitboxModule = require(Modules.MuchachoHitbox)
CombatEvent.OnServerEvent:Connect(function(Player, Value)
local Character = Player.Character
local BlockAnim = Character.Humanoid:LoadAnimation(Animations.Block)
if Player.Stunned.Value == false and Value == "Attack" then
local NewHitbox = HitboxModule.CreateHitbox()
local SlashClone = Effects.Slash:Clone()
local Connection = nil
SlashClone.Parent = Character.HumanoidRootPart.RootAttachment
SwingSound:Play()
--DB = true
Params.FilterDescendantsInstances = {Character}
NewHitbox.Visualizer = false
NewHitbox.Size = Vector3.new(6, 6, 6)
NewHitbox.CFrame = Character.HumanoidRootPart
NewHitbox.Offset = CFrame.new(0, 0, -3)
NewHitbox.OverlapParams = Params
Player.Stunned.Value = true
Combo.Value += 1
if Combo.Value >= 3 then
Combo.Value = 1
end
local AnimTrack = Character.Humanoid:LoadAnimation(Animations:FindFirstChild("Slash"..Combo.Value))
AnimTrack:Play()
--Connection = NewHitbox.Touched:Connect(function(hit, hum)
-- local HitClone = Effects.Hit:Clone()
-- local HitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
-- local StunFunction = nil
-- if HitPlayer and HitPlayer.Stunned.Value == false then
-- if StunFunction ~= nil then
-- task.cancel(StunFunction)
-- end
-- HitPlayer.Stunned.Value = true
-- HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
-- hum:TakeDamage(10)
-- HitSound:Play()
-- hum:LoadAnimation(Animations:FindFirstChild("Hit"..Combo)):Play()
-- task.delay(0.25, function()
-- HitClone:Destroy()
-- end)
-- StunFunction = task.spawn(Stun, HitPlayer)
-- end
--end)
Connection = NewHitbox.Touched:Connect(function(hit, hum)
local HitClone = Effects.Hit:Clone()
local HitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if HitPlayer and HitPlayer.Stunned.Value == false and StunCooldown[HitPlayer.UserId] == nil then
-- Verifica se o jogador não está atordoado e se o cooldown está zerado
HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
hum:TakeDamage(10)
HitSound:Play()
hum:LoadAnimation(Animations:FindFirstChild("Hit"..Combo.Value)):Play()
task.delay(0.25, function()
HitClone:Destroy()
end)
Stun(HitPlayer) -- Chama a função Stun para aplicar o stun
elseif not HitPlayer then
HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
hum:TakeDamage(10)
HitSound:Play()
hum:LoadAnimation(Animations:FindFirstChild("Hit"..Combo.Value)):Play()
task.delay(0.25, function()
HitClone:Destroy()
end)
end
end)
NewHitbox:Start()
task.delay(0.27, function()
if Player.Stunned.Value == true then
Connection:Disconnect()
SlashClone:Destroy()
Player.Stunned.Value = false
NewHitbox:Destroy()
end
end)
--[[elseif Player.Stunned.Value == false and Value == "Block" then
BlockAnim:Play()
elseif Player.Stunned.Value == false and Value == "Unblock" then
BlockAnim:Stop()
end--]]
end)
(also yes, i already started working on a blocking system
But i still make a check if it is a player, so there’s no reason of why i should do that, also that will also catch for dummies, and dummies doesn’t have those atributtes, future i should do that tho
Lemme give you the line of maybe you haven’t read (oh sorry i gave the one with the blocking system, just ignore it):
local HitClone = Effects.Hit:Clone()
local HitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if HitPlayer and HitPlayer.Stunned.Value == false and StunCooldown[HitPlayer.UserId] == nil then
-- Verifica se o jogador não está atordoado e se o cooldown está zerado
if HitPlayer.Blocking.Value ~= true or HitPlayer.BlockHealth.Value <= 0 then
HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
hum:TakeDamage(10)
HitSound:Play()
hum:LoadAnimation(Animations:FindFirstChild("Hit"..Combo.Value)):Play()
task.delay(0.25, function()
HitClone:Destroy()
end)
Stun(HitPlayer) -- Chama a função Stun para aplicar o stun
elseif HitPlayer.Blocking.Value == true then
if HitPlayer.BlockHealth > 0 then
HitClone.Parent = hum.Parent.HumanoidRootPart.RootAttachment
HitSound:Play()
hum:LoadAnimation(Animations:FindFirstChild("Block"..Combo.Value)):Play()
task.delay(0.25, function()
HitClone:Destroy()
end)
end
end