Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I was reformatting this code in a module script but for some reason it refuses to load, why?

MY VERSION:

Server Script:

local ta
local tb

local function init()
ta, tb = require(script.Triangle)(Vector3.new(0, 0, 0), Vector3.new(10, 0, 10), Vector3.new(0, 0, 10), workspace, ta, tb)
end

init()

Module:

local Triangle do

	local ref = Instance.new('WedgePart') do
		ref.Material      = Enum.Material.SmoothPlastic
		ref.Reflectance   = 0
		ref.Transparency  = 0
		ref.Name          = ''
		ref.Anchored      = true
		ref.CanCollide    = false
		ref.CFrame        = CFrame
		ref.Size          = Vector3.new(0.25, 0.25, 0.25)
		ref.BottomSurface = Enum.SurfaceType.Smooth
		ref.TopSurface    = Enum.SurfaceType.Smooth
	end
	
	local function fromAxes(part, x, y, z)
		
		return CFrame.new(part.x, part.y, part.z, x.x, y.x, z.x, x.y, y.y, z.z, x.z, y.z, z.z)
	end
	
	
	function Triangle(a, b, c, parent, wb, wc)
		--Get the distance between each point
		local A, B, C = b - a, c - b,  c - a
		local A_Magnitude, B_Magnitude, C_Magnitude = A.magnitude, B.magnitude, C.magnitude
		
		
		--[[	If the distance between point "b" and "a" is less than the distance between point "c" and "a"	]]--
		--[[	If the distance between point "b" and "a" is less than the distance between point "c" and "b"	]]--
													--[[SWAP]]--
		--||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||--
		
		--[[	If the distance between point "c" and "b" is less than the distance between point "c" and "a"	]]--
		--[[	If the distance between point "c" and "b" is less than the distance between point "b" and "a"	]]--
													--[[SWAP]]--
		if A_Magnitude > C_Magnitude and A_Magnitude > B_Magnitude then
			c, a = a, c
		elseif B_Magnitude > C_Magnitude and B_Magnitude > A_Magnitude then
			a, b = b, a
		end
		
		A, B, C = b - a, c - a, c - b
		
		local out = Vector3.new().Cross(B, A).unit
		
		wb = wb or game.Clone(ref)
		wc = wc or game.Clone(ref)
		
		local bisectDirection = Vector3.new().Cross(C, out).unit
		local bisectLength = math.abs(Vector3.new().Dot(A, bisectDirection))
		local normal = C.magnitude
		
		wb.Size = Vector3.new(0, math.abs(Vector3.new().Dot(A, C))/normal, bisectLength)
		wc.Size = Vector3.new(0, bisectLength, math.abs(Vector3.new().Dot(A, C))/normal)
		
		C = -C.unit
		
		wb.CFrame = fromAxes((a + b)/2, -out, C, -bisectDirection)
		wc.CFrame = fromAxes((a + c)/2, -out, bisectDirection, C)
		
		wb.Parent = parent
		wc.Parent = parent
		
		return wb, wc
	end
end

return Triangle

OLD VERSION:

Server Script:

local a = workspace.A
local b = workspace.B
local c = workspace.C

local ta
local tb

local function update()
	ta, tb = require(script.Triangle)(a.Position, b.Position, c.Position, workspace, ta, tb)
end

a.Changed:Connect(update)
b.Changed:Connect(update)
c.Changed:Connect(update)

update()

Module:

local Triangle do
	local v3 = Vector3.new
	local cf = CFrame.new
	local abs = math.abs
	local cross = v3().Cross
	local dot = v3().Dot
	local clone = game.Clone
	
	local ref = Instance.new('WedgePart') do
		ref.Color         = Color3.fromRGB(200, 200, 200)
		ref.Material      = Enum.Material.SmoothPlastic
		ref.Reflectance   = 0
		ref.Transparency  = 0
		ref.Name          = ''
		ref.Anchored      = true
		ref.CanCollide    = false
		ref.CFrame        = cf()
		ref.Size          = v3(0.25, 0.25, 0.25)
		ref.BottomSurface = Enum.SurfaceType.Smooth
		ref.TopSurface    = Enum.SurfaceType.Smooth
	end
	
	local function fromAxes(p, x, y, z)
		return cf(
			p.x, p.y, p.z,
			x.x, y.x, z.x,
			x.y, y.y, z.y,
			x.z, y.z, z.z
		)
	end
	
	function Triangle(a, b, c, parent, wb, wc)
		local ab, ac, bc = b - a, c - a, c - b
		local abl, acl, bcl = ab.magnitude, ac.magnitude, bc.magnitude
		if abl > bcl and abl > acl then
			c, a = a, c
		elseif acl > bcl and acl > abl then
			a, b = b, a
		end
		ab, ac, bc = b - a, c - a, c - b
		local out = cross(ac, ab).unit
		wb = wb or clone(ref)
		wc = wc or clone(ref)
		local biDir = cross(bc, out).unit
		local biLen = abs(dot(ab, biDir))
		local norm = bc.magnitude
		wb.Size = v3(0, abs(dot(ab, bc))/norm, biLen)
		wc.Size = v3(0, biLen, abs(dot(ac, bc))/norm)
		bc = -bc.unit
		wb.CFrame = fromAxes((a + b)/2, -out, bc, -biDir)
		wc.CFrame = fromAxes((a + c)/2, -out, biDir, bc)
		wb.Parent = parent
		wc.Parent = parent
		return wb, wc
	end
end

return Triangle

It keeps returning Requested module experienced an error while loading

1 comment

0

Why do you have everything written as if it is a loop?

1 answer

This is the very reason why you should learn to script before using other people's scripts and modifying them. To answer your question:

change the first line from local Triangle do to local Triangle = {}

This discussion is preserved as it was. New answers, comments, and votes are closed.