DarkZone skill [code feedback]

This is a skill I did, which creates a black zone that predicts players who are close

https://gyazo.com/2c417c85bb09b789e329df7516b6d7df

List item

1. Trapped enemies are silenced (can’t use skills)
2. Whoever activates the skill is unstoppable (cannot be stunned or arrested by another player)
3.Trapped enemies cannot be pulled into other players’ darkzones, and cannot fly or jump

Code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DarkZone = ReplicatedStorage.Skills.Impure.DarkZone.Remotes.DarkZone
local Meshes = script.Meshes
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local trainingm = require(game.ServerStorage.TrainingFiles.TrainingModule)
local StunEvent = game.ServerStorage.Events.Stun
local stun = require(game.ServerStorage.BadEffects.Stun)
local Silence = require(game.ServerStorage.BadEffects.Silence)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Unstopable = require(game.ServerStorage.GoodEffects.Unstoppable)
local PlayerD
local model
local Stuned = {}
local tween2
local tween


local removeTrap = coroutine.wrap(function()
	while model.Parent do
		wait(RunService.Heartbeat)
		 print("Waiting ")
	end
	for _,p in pairs(Players:GetChildren()) do
		if p.Character:FindFirstChild("Traped") then
		   p.Character.Traped:Destroy()
		end
	end
end)


local function untrapP()
	Silence.Remove(Stuned)
	stun.RemoveStun(Stuned)
	Stuned = nil
end


StunEvent.Event:Connect(function(p)
	if p == PlayerD.Character then
	   untrapP()
	   if tween2 ~= nil then
	    tween2:Cancel()
	   end
	   workspace:FindFirstChild(p.Name.." DZone"):Destroy()
	end
end)

local function trapPlayers(huma,character)
	Stuned = stun.StunPlayersAround(character,huma,10)
	Silence.Silence(Stuned)
	print("HIII")
	for index = 1, #Stuned do
		print(index)
		local traped = Instance.new("BoolValue",character)
		traped.Name = "Traped"
		local playerStuned = Stuned[index]
		local humanoid = playerStuned:WaitForChild("HumanoidRootPart")
		local goal = {}
        goal.CFrame = Hbox.CFrame
        local info = TweenInfo.new(1)  
        local tween4 = TweenService:Create(humanoid,info,goal)
        tween4:Play()
	end
	
end

	
local function darkZoneF(player)
	
	local s, e = pcall(function()
	
	local usingSkill = Instance.new("BoolValue")
    usingSkill.Name = "usingSkill"
    usingSkill.Value = true
    usingSkill.Parent = player
	  
    local Position
    local Character = player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
    local HumanoidRP = Character:WaitForChild("HumanoidRootPart")

    local Track = Humanoid:LoadAnimation(script.DZone1)
    local Track2 = Humanoid:LoadAnimation(script.DZone2)
    Track2:Play()
    Track:Play()  

    local Folder = Instance.new("Folder",workspace)
    Folder.Name = player.Name.." DZone"
    Folder.Parent = workspace

    Debris:AddItem(Folder,20)

    model = Instance.new("Model", game.Workspace)
    model.Parent = Folder

    removeTrap()

    Debris:AddItem(model,10)
    
    local mDark = Meshes:WaitForChild("MDarkZone"):Clone()
    mDark.CFrame = Character:WaitForChild("LeftHand").CFrame * CFrame.new(0.8,0,-0.1)
    mDark.Parent = model
   
    local weld = Instance.new("WeldConstraint")
    weld.Parent = Folder
    weld.Part0 = mDark
    weld.Part1 = Character:WaitForChild("LeftHand")
   
    wait(1)
   
    local goal = {}
    goal.Transparency = mDark.Transparency - 1
    local info = TweenInfo.new(0.2)  
    local tween = TweenService:Create(mDark,info,goal)
    tween:Play()
   
    HumanoidRP.Anchored = false
   
    wait(2)
  
    Hbox = Meshes:WaitForChild("Hbox"):Clone()
    Hbox.CFrame = HumanoidRP.CFrame + Vector3.new(0,70,0)
    Hbox.Parent = model

    model.PrimaryPart = Hbox

    Position = Vector3.new(HumanoidRP.CFrame.X, HumanoidRP.CFrame.Y,HumanoidRP.CFrame.Z)
    model:MoveTo(Position)

    trapPlayers(HumanoidRP,Character)

    Unstopable.Set(Character)

    HumanoidRP.Anchored = true

    local goal = {}
    goal.CFrame = Hbox.CFrame
    local info = TweenInfo.new(1)  
    tween2 = TweenService:Create(HumanoidRP,info,goal)
    tween2:Play()

    Hbox.CanCollide = false
 
    weld:Destroy()

    mDark.CFrame = Hbox.CFrame

    local weld2 = Instance.new("WeldConstraint")
    weld2.Part0 = Hbox
    weld2.Part1 = mDark
    weld2.Parent = Folder

    tween2.Completed:Wait()

    local Track3 = Humanoid:LoadAnimation(script.DZone3)
    Track3:Play()

    Track3.Stopped:Wait()

    Track2:Stop()

    local goal = {}
    goal.Size = mDark.Size + Vector3.new(70, 70, 70)
    local info = TweenInfo.new(0.2)  
    tween = TweenService:Create(mDark,info,goal)
    tween:Play()
   
    tween.Completed:Wait()

    usingSkill:Destroy()

    mDark.CFrame = Hbox.CFrame

    Hbox.CanCollide = true

    mDark.ParticleEmitter.Enabled = false

    mDark.ParticleBigZone.Enabled = true
   
    mDark.Anchored = true

    mDark.CanCollide = true

    HumanoidRP.Anchored = false

    untrapP()
   

    end)
   
    if not s then 
	   print("wtfffffff")
	   if workspace:FindFirstChild(player.Name.." DZone") then
		  workspace:FindFirstChild(player.Name.." DZone"):Destroy()
	   end
	   if Stuned[player.Character] then
		
	   else
		 player.Character:WaitForChild("HumanoidRootPart").Anchored = false
	   end
    end
end


local function checkEnable(player)
    
	if trainingm.GetTraining(player)["Endurance"] ~= "No" 
	or trainingm.GetTraining(player)["Mind"] == "Auto" 
	or player:FindFirstChild("usingSkill") 
	or workspace:FindFirstChild(player.Name.." DZone")
	or Silence.Get(player.Character) then
		 print("??????????????????")
	else
		print("wwatatat")
		PlayerD = player
		player.Character:WaitForChild("HumanoidRootPart").Anchored = true
		darkZoneF(player)
	end
		
end

DarkZone.OnServerEvent:Connect(checkEnable)

Please give a feedback on code, and say what i should better

In your for loops, you use index and table separately. (Table[i])

Instead, do in pairs()

ipairs if your indices are simply numbers starting from 1 (AKA your tables are arrays).

1 Like

@anon66957764 is right in the sense that ipairs are better here than pairs, but there’s nothing wrong with using a numeric loop here.

1 Like

ipairs and numeric loops basically work the same. Personally I would use ipairs, as it’s shorter and clearer, but nothing wrong with numeric loops. Sometimes, they’re the only option (like if you want to go backwards)

1 Like

If you use the # operator to find the length, not really.

If you’re using pack(...) for example in a variadic function, and use the n field, then a numeric loop would work better, since ipairs would miss all nils. If you have a nil in the middle, then ipairs would stop at that.

pack(1, nil, 2, nil) = {1, nil, 2, nil, n = 4}

If you mean pairs and ipairs, that isn’t exactly true, there are some differences. That aside, pairs are meant for dictionaries (though will still work).

1 Like

I never said that. Numeric loops that use for i = 1, #table are effectively the same as for i, _ in ipairs(table) since both # and ipairs miss nils.

ipairs only counts up from 1 and so on. If you want guaranteed order, use that. pairs iterates over every key in the table, but with no guaranteed order.

1 Like