How do I get this variable from a function in a module script?

I’m trying to make a substitute for the roblox collisions myself but I’ve gotten stuck on this one bit.

Here’s what the workspace looks like, just in case.

Here’s the scripts;

ImprovisedCollision | Module Script

local module = {}

-- IMPORTANT --
-- this module is made to replace the roblox collisions with a more reliable and possibly efficient solution. it's made to 
-- solve the problem with HIGH speed collisions, not low speed. as in, there will be a visible delay with low speed but not
-- as apparent at high speeds. (please use for intended purpose)

local tool = script.Parent.Parent

local collidedis = 1 -- the distance at which the part is considered "colliding", the more the better at high speeds, depending on
-- the delay you have in the script that of which is calling this

module.CollisionPart = tool.Blade
local collisionPart = module.CollisionPart.Name

module.checkCollision = function(Part, collisionWith)
	for _, Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("Part") or Part:IsA("UnionOperation") or Part:IsA("MeshPart") or Part:IsA("Wedge") or Part:IsA("Cylinder") then
			if (module.CollisionPart.Position - Part.Position).Magnitude < collidedis and Part.Name ~= collisionPart then
				collisionWith = Part
				return collisionWith
			elseif (module.CollisionPart.Position - Part.Position).Magnitude > collidedis then
				-- idk do whatever you want here
			end
		end
	end
end



return module

Collision | Server Script


local ImprovisedCollis = require(script.Parent.ImprovisedCollision)
local interval = 0.01

while wait(interval) do
	ImprovisedCollis.checkCollision(ImprovisedCollis.collisionWith)
	print(ImprovisedCollis.collisionWith)
end

I’m basically trying to get the collisionWith variable from the module script function.
(Oh yeah btw, it prints nil in the server script.)

If someone could help, I’d greatly appreciate it.

Thanks,

  • Luke

There is nothing called collisionWith in module.
Expect a module.collisionWith
got a normal collisionWith instead.

I’m kinda confused, do you mind rewording possibly? Like, how exactly do I fix it? (Someone please help)

1 Like

Create a new key/field in the ‘module’ table and assign it the result of the operation being looped. Then all you need to do is index this key/field in the requiring script to fetch the latest value (of the previously mentioned operation).

You havent created anything called module.collisionWith.
You created collisionWith that is not parented to the mainmodule

Here this code may work:

local module = {}
-- IMPORTANT --
-- this module is made to replace the roblox collisions with a more reliable and possibly efficient solution. it's made to 
-- solve the problem with HIGH speed collisions, not low speed. as in, there will be a visible delay with low speed but not
-- as apparent at high speeds. (please use for intended purpose)

local tool = script.Parent.Parent

local collidedis = 1 -- the distance at which the part is considered "colliding", the more the better at high speeds, depending on
-- the delay you have in the script that of which is calling this

module.CollisionPart = tool.Blade
module.collisionWith = nil
local collisionPart = module.CollisionPart.Name

module.checkCollision = function(Part, collisionWith)
	for _, Part in pairs(workspace:GetDescendants()) do
		if Part:IsA("Part") or Part:IsA("UnionOperation") or Part:IsA("MeshPart") or Part:IsA("Wedge") or Part:IsA("Cylinder") then
			if (module.CollisionPart.Position - Part.Position).Magnitude < collidedis and Part.Name ~= collisionPart then
				module.collisionWith = Part
				return module.collisionWith
			elseif (module.CollisionPart.Position - Part.Position).Magnitude > collidedis then
				-- idk do whatever you want here
			end
		end
	end
end



return module

Ignore those other posts. You are returning collisionWith so you just need to save it as a variable.

local returnedValue = ImprovisedCollis.checkCollision(part)
print(returnedValue)

Also get rid of collisionWith in the parentheses where you create the function.

I tried that but there isn’t any part variable so there’s not much for the script to print.

I found another solution to my problem. (same code i had originally in the module)


local ImprovisedCollis = require(script.Parent.ImprovisedCollision)
local interval = 0.01

while wait(interval) do
	ImprovisedCollis.checkCollision(nil, script.Parent.Parent.Blade)
	if ImprovisedCollis.checkCollision() ~= nil then
		print(ImprovisedCollis.checkCollision())
	end
end