Not able to create hitboxes for multiple players

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    the script i have works for one player, but it breaks when multiple players try and swing at once, not creating a hitbox for the second player. I have tried using task.spawn(), but it did not help, and i am not entirely aware of my options, as this is my first time trying to do something like this.
    ‘’’
    local detector = game:GetService(“ReplicatedStorage”).Damage
    local debounce = false
    local function hitboxes(player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
    if debounce == false then
    debounce = true
    times = tonumber(times)
    local truealreadyhit = {}
    task.wait(tonumber(delay))
    for count = 0, times, 1 do
    local hit = Instance.new(“Part”)
    local velocityx = player.Character.Velocityx.Value
    local velocityxmult = velocityx/22
    local velocityz = player.Character.Velocityz.Value
    local velocityzmult = velocityz/22
    game:GetService(“Debris”):AddItem(hit, 2)
    hit.Size = Vector3.new(tonumber(sizex),tonumber(sizey),tonumber(sizez))
    local offset = player.Character.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,tonumber(offsetz)))
    hit.CFrame = offset + Vector3.new((5velocityxmult), 0, (5velocityzmult))
    local param = OverlapParams.new()
    param.FilterType = Enum.RaycastFilterType.Exclude
    param.FilterDescendantsInstances = {player.Character}
    hit.Anchored = true
    hit.Parent = game.Workspace
    hit.CanCollide = false
    hit.Transparency = 0.8
    local hitparts = workspace:GetPartsInPart(hit, param)
    local alreadyhit = {}
    for i,v in hitparts do
    local humanoid = v.Parent:FindFirstChild(“Humanoid”)
    if humanoid then
    if not table.find(alreadyhit, humanoid) then
    if not table.find(truealreadyhit, humanoid) then
    local kbfb = player.Character.HumanoidRootPart.CFrame.LookVector * tonumber(knockbackback)
    humanoid.RootPart:ApplyImpulse(Vector3.new(0, tonumber(knockbackup), 0)+kbfb)
    humanoid:TakeDamage(damage)
    if knockbackragdoll == true then
    humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
    humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
    end
    table.insert(alreadyhit, humanoid)
    table.insert(truealreadyhit, humanoid)
    end
    end
    end
    end
    wait(0.025)
    end
    wait(tonumber(cooldown))
    debounce = false
    end
    end
    detector.OnServerEvent:Connect(function(player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
    task.spawn(hitboxes, player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
    end)
    ‘’’

sorry code should be

local detector = game:GetService("ReplicatedStorage").Damage
local debounce = false
local function hitboxes(player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
if debounce == false then
	debounce = true
	times = tonumber(times)
	local truealreadyhit = {}
	task.wait(tonumber(delay))
	for count = 0, times, 1 do
	local hit = Instance.new("Part")
	local velocityx = player.Character.Velocityx.Value
	local velocityxmult = velocityx/22
	local velocityz = player.Character.Velocityz.Value
	local velocityzmult = velocityz/22
	game:GetService("Debris"):AddItem(hit, 2)
	hit.Size = Vector3.new(tonumber(sizex),tonumber(sizey),tonumber(sizez))
	local offset = player.Character.HumanoidRootPart.CFrame:ToWorldSpace(CFrame.new(0,0,tonumber(offsetz)))
	hit.CFrame = offset + Vector3.new((5*velocityxmult), 0, (5*velocityzmult))
	local param = OverlapParams.new()
	param.FilterType = Enum.RaycastFilterType.Exclude
	param.FilterDescendantsInstances = {player.Character}
	hit.Anchored = true
	hit.Parent = game.Workspace
	hit.CanCollide = false
	hit.Transparency = 0.8
	local hitparts = workspace:GetPartsInPart(hit, param)
	local alreadyhit = {}
		for i,v in hitparts do
			local humanoid = v.Parent:FindFirstChild("Humanoid")
			if humanoid then
				if not table.find(alreadyhit, humanoid) then
					if not table.find(truealreadyhit, humanoid) then
					local kbfb = player.Character.HumanoidRootPart.CFrame.LookVector * tonumber(knockbackback)
					humanoid.RootPart:ApplyImpulse(Vector3.new(0, tonumber(knockbackup), 0)+kbfb)
					humanoid:TakeDamage(damage)
					if knockbackragdoll == true then
						humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
						humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
					end
					table.insert(alreadyhit, humanoid)
					table.insert(truealreadyhit, humanoid)
					end
				end
			end
		end
	wait(0.025)
	end
		wait(tonumber(cooldown))
		debounce = false
	end
end
detector.OnServerEvent:Connect(function(player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
	task.spawn(hitboxes, player, sizex, sizey, sizez, delay, cooldown, damage, times, offsetz, knockbackup, knockbackback, knockbackragdoll)
	print("skibid")
	end)

please use task.wait instead of wait… u might want to break this down into multiple functions because the “hitboxes” function isn’t very readable.

okay. Is there anything besides that I could do to make it work for multiple players?

making it somewhat readable should have been the first order of business; if something breaks, the problematic portion can be found easily

Fair point :face_with_open_eyes_and_hand_over_mouth: . I fixed the problem by making it a local script but i will keep readability in mind in the future. Thank you regardless!