I’m making a battlegrounds system and I have a problem when I create a second character. I want to copy all the previous attacks of the first character to the second character but I get an error.
Their scripts are essentially the same, but for some reason they break when two characters are involved
error script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local CharacterConfigurations = require(ReplicatedStorage.SharedModules.Configurations.CharacterConfigurations)
local AttackDebounces = {}
local module = {}
module.Modules = {}
local AttackHitboxes
local AttackData = nil
for _, v in CharacterConfigurations do
if v.CharacterName == script.Parent.Name then
AttackData = v
break
end
end
module.AttackFunction = function(Player)
if Player.Values.IsAttacking.Value == false and not AttackDebounces[Player] then
AttackDebounces[Player] = true
Player.Values.IsAttacking.Value = true
task.delay(AttackData.Attacks[tonumber(script.Name)].AttackDuration, function()
Player.Values.IsAttacking.Value = false
end)
task.delay(AttackData.Attacks[tonumber(script.Name)].AttackDebounce, function()
AttackDebounces[Player] = nil
end)
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local Humanoid = Character.Humanoid
local LoadedPunchAnimation = Humanoid.Animator:LoadAnimation(script.PunchAnimation)
LoadedPunchAnimation:Play()
local function HitPlayer(VictimCharacter)
local Victim = Players:GetPlayerFromCharacter(VictimCharacter)
if Victim then
VictimCharacter.IsRagdoll.Value = true
local KnockbackAttachment = Instance.new("Attachment")
KnockbackAttachment.Parent = VictimCharacter.HumanoidRootPart
local KnockbackLinearVelocity = Instance.new("LinearVelocity")
KnockbackLinearVelocity.Parent = VictimCharacter.HumanoidRootPart
KnockbackLinearVelocity.MaxForce = 9999999
KnockbackLinearVelocity.Attachment0 = KnockbackAttachment
KnockbackLinearVelocity.VectorVelocity = (HumanoidRootPart.CFrame.LookVector * 35) + Vector3.new(0, 50, 0)
VictimCharacter.Humanoid.Health -= 20
local Explosion = script.ExplosionParticle.Attachment:Clone()
Explosion.Parent = VictimCharacter.HumanoidRootPart
for _,v in Explosion:GetDescendants() do
if v.ClassName ~= "ParticleEmitter" then
continue
end
v:Emit(v:GetAttribute("EmitCount"))
end
task.spawn(function()
repeat task.wait() until VictimCharacter.Grounded.Value == false
repeat task.wait() until VictimCharacter.Grounded.Value == true
ReplicatedStorage.Events.CreateCrater:FireAllClients(VictimCharacter.HumanoidRootPart.Position, 12, 12, 5)
ReplicatedStorage.Events.ShakeCamera:FireClient(Player, "Bump")
end)
task.delay(0.2, function()
KnockbackLinearVelocity:Destroy()
KnockbackAttachment:Destroy()
task.wait(5)
Explosion:Destroy()
VictimCharacter.IsRagdoll.Value = false
end)
end
end
LoadedPunchAnimation:GetMarkerReachedSignal("Hit"):Connect(function()
local Result = AttackHitboxes.CreateHitbox(Character, Vector3.new(9, 9, 5), 10, true)
if Result ~= nil then
HitPlayer(Result)
local PunchSound = script.PunchSound:Clone()
PunchSound.Parent = HumanoidRootPart
PunchSound:Play()
task.wait(2)
PunchSound:Destroy()
end
end)
return true
else
return false
end
end
function module.Init()
AttackHitboxes = module.Modules.AttackHitboxes
end
return module
Everything works when there is only one character, but when I want to add another one, everything breaks down