What do you want to achieve?
I want to save two positions of an attachment at two times to they cast a ray between them for hitbox purposes.
What is the issue?
It only prints one position, no matter where the part, or “weapon” is at in the world. Every key in the “positions” table is the same position, the position where the part is placed in the workspace. Moving it does nothing.
What solutions have you tried so far?
Nothing, this should be working unless I’m super dumb or something.
local module = {}
function module.CreateHitbox(data)
local weapon: Model = data.weapon
local attachments = {}
--Add attachments to table
for _, v in pairs(weapon:GetChildren()) do
if v.Name == "DamagePoint" then
table.insert(attachments, v)
end
end
for _, v: Attachment in ipairs(attachments) do
task.spawn(function()
local positions = {}
table.insert(positions, v.WorldCFrame)
task.wait(.1)
table.insert(positions, v.WorldCFrame)
print(positions)
end)
end
end
return module
Hey, your issue is a simple mistake with placement.
for _, v: Attachment in ipairs(attachments) do
task.spawn(function()
local positions = {}
table.insert(positions, v.WorldCFrame)
task.wait(.1)
table.insert(positions, v.WorldCFrame)
print(positions)
end)
end
The error lies in the 3rd (actual) line, when you define the table.
The table is defined within the loop, this means that each run of the loop the table is reset
This is because variables are only around as long as they are defined.
If you place this table outside the loop, it will exist in each run.
You should move local positions = {} to outside of the for _, v:Attachment in ipairs(attachments) do
local module = {}
function module.CreateHitbox(data)
local weapon: Model = data.weapon
local attachmentPositions = {}
local function logAttachments()
for _, v in pairs(weapon:GetChildren()) do
if v:IsA("Attachment") then
task.spawn(function()
attachmentPositions[v.Name] = {}
attachmentPositions[v.Name][1] = v.WorldPosition
task.wait(.1)
attachmentPositions[v.Name][2] = v.WorldPosition
print(attachmentPositions)
end)
end
end
end
logAttachments()
end
return module
Ok, interesting development. It doesn’t even print the worldposition in the script firing the module function!!
local hitboxmodule = require(game.ReplicatedStorage.HitboxModule)
while true do
hitboxmodule.CreateHitbox({
weapon = script.Parent
})
print(script.Parent.DamagePoint.Position)
task.wait(1)
end
obviously this is disgusting code but bear with me
Nevermind guys, I got it working. Heres the code for anyone wondering:
local module = {}
function module.CreateHitbox(data)
local weapon: Model = data.weapon
local attachments = {}
local hitlist = {}
local function logAttachments()
for _, v in pairs(weapon:GetChildren()) do
if v:IsA("Attachment") then
task.spawn(function()
table.insert(attachments, v)
end)
end
end
end
local function castRay(positions)
end
local function getPositions(attachment: Attachment)
local positions = {}
local positionOne = attachment.WorldPosition
table.insert(positions, positionOne)
task.wait(.05)
local positionTwo = attachment.WorldPosition
table.insert(positions, positionOne)
return positions
end
logAttachments()
for i, v in ipairs(attachments) do
task.spawn(function()
local positions = getPositions(v) -- maybe change to v idk
print(positions)
local ray = castRay(positions)
end)
end
for i, v in pairs(hitlist) do
print("hit"..i, v)
end
end
return module