Logging Positions Not Working

  1. 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.

  2. 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.

  3. 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

Thanks

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

I’ve edited the code like so, to no avail:

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

You changed from table.insert() to attachmentPositions[]

This means the key [v.Name] is being constantly updated.

  • If I recall correctly, all of your attachments have the same name.
  • You are consistently setting v.Name to equal something new every run.

You should use table.insert() instead, because it will assign each attachment a new index.

Also, move your print() to outside the function (after logattachments) because it will only print based on the current run, not the full table.

The idea is that the table structure will be like this:

local attachmentPositions = {
     attachment = {
          position1 = whatever
          position2 = blah blah
     }
}

Which i do not believe to be possible using that method

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

it prints it but not correctly

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.