For loop doesn't do anything (REPOST)

The title describes the problem im having

just in case if you dont understand what im trying to do is im using a Touched event and a for loop as a debounce to parent an attachment and an AlignPosition

Here is the code



local catched = {}
local allPoses = {}


for i,v in pairs(script.Parent.Parent.Parent.Poses:GetChildren()) do
	table.insert(allPoses, v.Attachment1)
	print(allPoses)
end


script.Parent.Touched:Connect(function(hit)
	
	local hum = hit.Parent:FindFirstChild("Humanoid")
	
	if hum then
		
		
		print("nice a humanoid")
		local character = hit.Parent.HumanoidRootPart
		
		for _,v in pairs(catched) do
			if hit.Parent.Name ~= v then
				table.insert(catched,hit.Parent.Name) --insert player name into the table so it doesnt repeat again
				hum.WalkSpeed = 0
				hum.JumpPower = 0

				local characterAttach = Instance.new("Attachment",character)
				characterAttach.Name = "Attachment0"



				local AlignPosition = Instance.new("AlignPosition",character)

				AlignPosition.Attachment0 = character.Attachment0
				AlignPosition.Attachment1 = allPoses[math.random(1,#allPoses)]
				AlignPosition.Responsiveness = 125
			
			end
		end
			
			
			


			--ignore this
			--[[
			hum.WalkSpeed = 0
			hum.JumpPower = 0

			local characterAttach = Instance.new("Attachment",character)
			characterAttach.Name = "Attachment0"



			local AlignPosition = Instance.new("AlignPosition",character)

			AlignPosition.Attachment0 = character.Attachment0
			AlignPosition.Attachment1 = allPoses[math.random(1,#allPoses)]
			AlignPosition.Responsiveness = 125
			--]]
		
	else
		return -- return nothing if player name is already in the table
	end
	
	
	
	
end)


for some reason after it prints “nice humanoid” it doesn’t give me any errors nor do anything

2 Likes

Can you verify there is at least one element in the catched table?

Add a print directly following the for loop statement

It indeed did print my name in the table before i added the for loop(it printed my name in the table 500 times) so the same attachments and alignposition doesn’t get parented to the character again

but right now it doesn’t

do for loops break when using events or something?

also the only thats being printed right now in the output is this:
Screenshot_1

1 Like

there we go again.

the fear of not getting a response after spending 2 minutes to confirm your question is truly horrifying

will i ever find a solution to this problem?

print(“a”) above this line to see if the loop even does anything.

1 Like

Unfortunately it doesn’t print i don’t know what’s wrong with the code.

1 Like

man i hate lua chracter limit

Hey, at what point does the script stop working?

after these lines (before the for loop)

Try:

if hit.Parent.Name ~= v.Name then

If there’s something else wrong, do let me know.

It didn’t work again

same output “nice a humanoid” and after that nothing happens

1 Like

Try this.

local catched = {}
local allPoses = {}


for i,v in pairs(script.Parent.Parent.Parent.Poses:GetChildren()) do
	table.insert(allPoses, v.Attachment1)
	print(allPoses)
end


script.Parent.Touched:Connect(function(hit)

	local hum = hit.Parent:FindFirstChild("Humanoid")

	if hum then


		print("nice a humanoid")
		local character = hit.Parent.HumanoidRootPart

		for _,v in pairs(catched) do
			if not table.find(catched, hit.Parent.Name) then
				table.insert(catched,hit.Parent.Name) --insert player name into the table so it doesnt repeat again
				hum.WalkSpeed = 0
				hum.JumpPower = 0

				local characterAttach = Instance.new("Attachment",character)
				characterAttach.Name = "Attachment0"



				local AlignPosition = Instance.new("AlignPosition",character)

				AlignPosition.Attachment0 = character.Attachment0
				AlignPosition.Attachment1 = allPoses[math.random(1,#allPoses)]
				AlignPosition.Responsiveness = 125

			end
		end





		--ignore this
			--[[
			hum.WalkSpeed = 0
			hum.JumpPower = 0

			local characterAttach = Instance.new("Attachment",character)
			characterAttach.Name = "Attachment0"



			local AlignPosition = Instance.new("AlignPosition",character)

			AlignPosition.Attachment0 = character.Attachment0
			AlignPosition.Attachment1 = allPoses[math.random(1,#allPoses)]
			AlignPosition.Responsiveness = 125
			--]]

	else
		return -- return nothing if player name is already in the table
	end




end)

Either I’m stupid, or you forgot to actually set something in the {catched} table. Of course, the for loop won’t run when the # table is equal to 0. It’s the same thing as running for i = 0,0 do. You’re only changing the table in the for loop.

1 Like

Didn’t work and i understand why it didnt work

table.find() only returns the index of the table not the value itself

what could i do for it to work?

the catched table’s whole purpose is to store names of the players who touched the part nothing more.

1 Like

This isn’t always the case, which is why the third argument is optional. Try this, and set the variables to whatever you need. (At least I don’t think that it’s always the case.)

local Part = script.Parent

local HitHumanoids = {}

local OverParams = OverlapParams.new()
local Filter = {Part}
OverParams.FilterType = Enum.RaycastFilterType.Exclude

local function GetPartsInHitbox()
	OverParams.FilterDescendantsInstances = Filter
	local PartsInBox = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size, OverParams)
	for i, v in PartsInBox do
		if v.Parent:IsA("Model") and v:IsA("BasePart") and v.Parent:FindFirstChild("Humanoid") and not table.find(HitHumanoids, v.Parent) then
			local Rig = v.Parent
			local BaseParts = v
			local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
			if Humanoid and Humanoid.Health > 0 then
				Humanoid.WalkSpeed = 0
				Humanoid.JumpPower = 0
				table.insert(HitHumanoids, Rig)
				task.delay(3, function()
					table.remove(HitHumanoids, table.find(HitHumanoids, Rig))
				end)
			end
		end
	end
end

while true do
	task.wait()
	GetPartsInHitbox()
end

Sorry to tell you this but this script you made doesn’t even change the WalkSpeed or the JumpPower.

I’m not sure what your issue is then. This script worked fine for me. Try rewriting your code altogether.

ohhh hold on i think i got it

the v.Parent

I couldn’t thank you enough this works and here’s the final version of the code

local Part = script.Parent

local HitHumanoids = {}

local OverParams = OverlapParams.new()
local Filter = {Part}
OverParams.FilterType = Enum.RaycastFilterType.Exclude

local allPoses = {}


for i,v in pairs(script.Parent.Parent.Parent.Poses:GetChildren()) do
	table.insert(allPoses, v.Attachment1)
	print(allPoses)
end


local function GetPartsInHitbox()
	OverParams.FilterDescendantsInstances = Filter
	local PartsInBox = workspace:GetPartBoundsInBox(Part.CFrame, Part.Size, OverParams)
	for i, v in PartsInBox do
		if v.Parent:IsA("Model") and v:IsA("BasePart") and v.Parent:FindFirstChild("Humanoid") and not table.find(HitHumanoids, v.Parent) then
			local Rig = v.Parent
			local BaseParts = v
			local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
			if Humanoid and Humanoid.Health > 0 then
				print("yes")
				table.insert(HitHumanoids, Rig)
				
				local characterAttach = Instance.new("Attachment",v.Parent.HumanoidRootPart)
				characterAttach.Name = "Attachment0"



				local AlignPosition = Instance.new("AlignPosition",v.Parent.HumanoidRootPart)

				AlignPosition.Attachment0 = v.Parent.HumanoidRootPart.Attachment0
				AlignPosition.Attachment1 = allPoses[math.random(1,#allPoses)]
				AlignPosition.Responsiveness = 125
				task.delay(3, function()
					table.remove(HitHumanoids, table.find(HitHumanoids, Rig))
				end)
			end
		end
	end
end

while true do
	task.wait()
	GetPartsInHitbox()
end

(even though it still parents the instance over and over again to the character)

1 Like