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
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:
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.
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
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)