local functions = {}

local items = require(script.ItemList)

--GetItemInfo(name) returns the table of data added to the list with :AddItem() (excluding name, which is specified)
function functions:GetItemInfo(name)
	return items[name]
end

--Returns the table of items added to the list with :AddItem()
function functions:GetAllItems()
	return items
end

--Creates a model with transparency 'transparency' added to all parts' inside current transparency.
function functions:CreateModel(name,transparency,cancollide)
	if cancollide == nil then
		cancollide = true
	end
	local model = items[name][2]:Clone()
	model:MakeJoints()
	local function transparent(model)
		for i, v in pairs(model:GetChildren()) do
			if v:IsA('BasePart') then
				v.Transparency = math.min(v.Transparency + transparency, 0.9)
				if not cancollide then
					v.CanCollide = false
				end
			end
		end
	end
	transparent(model)
	return model
end

--This moves the model 'model' to the pos. Snap (in studs) is the grid that it will align to. 
--The restrictive bounds prevent the model from moving further outside of them. Upperleftcorner is the top left corner of the restrictive bounds.
--Lowerrightcorner is the lower left corner of the restrictive bounds. 'y' is the y the height of the base, IT MUST INCLUDE HALF OF THE BASE'S HEIGHT 
--(base.Position + base.Size.Y/2)
function functions:MoveModelToPos(hit,model,snap,upperleftcorner,lowerrightcorner,y)
	lowerrightcorner = lowerrightcorner
	local function clamp(x,y,z)
		return math.max(math.min(x,z),y)
	end
	local ysize,xsize = model.PrimaryPart.Size.Z,model.PrimaryPart.Size.X
	hit = hit.unit * math.min(hit.magnitude,1000)
	local xsnap = clamp(math.floor(hit.X/snap) * snap,upperleftcorner.X,lowerrightcorner.X-xsize)
	local zsnap = clamp(math.floor(hit.Z/snap) * snap,upperleftcorner.Y,lowerrightcorner.Y-ysize)
	model:SetPrimaryPartCFrame(CFrame.new(xsnap+xsize/2,y+model.PrimaryPart.Size.Y/2,zsnap+ysize/2))
end

--AllModels are all the items currently placed, it can or cannot include the model that is being checked (it will be removed). 
--'model' is the model in which it is checking for collisions for. 'otherIgnores' is a table of instances to ignore when checking (AllModels already does that, 
--but otherIgnores is for stuff like the baseplate or scenery or something like that.
function functions:CanPlace(AllModels,model,otherIgnores)
	local ignore = {}
	table.insert(ignore,model)
	for i, v in pairs(otherIgnores) do
		table.insert(ignore,v)
	end
	local region = Region3.new(model.PrimaryPart.Position - model.PrimaryPart.Size/2 + Vector3.new(0.05,0.05,0.05), model.PrimaryPart.Position + model.PrimaryPart.Size/2 - Vector3.new(0.05,0.05,0.05))
	for i, v in pairs(AllModels) do
		if v ~= model then
			for i, p in next, v:GetChildren() do
				if p ~= v.PrimaryPart then
					table.insert(ignore,p)
				end
			end
		end
	end
	local parts = workspace:FindPartsInRegion3WithIgnoreList(region,ignore,100)
	local count = #parts
	if count == 0 then
		return true
	else
		return false
	end
end


--Requires an table of items, following this format: {[ItemName] = Model}. OriginCFrame is the essentially, the CFrame of the part in which all the models are placed on.
function functions:GetPlacementData(itemtable,originCFrame)
	local t = {}
	for i, v in pairs(itemtable) do
		local obj = originCFrame:toObjectSpace(v.PrimaryPart.CFrame)
		local pos = obj.p
		local rot = Vector3.new(obj:toEulerAnglesXYZ())/math.pi
		t[tostring(i)] = {x=pos.X,y=pos.Y,z=pos.Z,x2=math.floor(rot.X),y2=math.floor(rot.Y),z2=math.floor(rot.Z)}
	end
	return t
end

--Requires an item data table following the format returned by :GetPlacementData(). OriginCFrame can be different 
--(different bases when the player rejoins if you are loading this, etc.). As such, this is meant to be used in conjunction with :GetItemData().
function functions:LoadPlacementData(itemtable,originCFrame)
	local models = {}
	for i, v in pairs(itemtable) do
		local model = items[tostring(i)][2]:Clone()
		model:MakeJoints()
		model:SetPrimaryPartCFrame(originCFrame:toWorldSpace(CFrame.new(v.x,v.y,v.z)*CFrame.Angles(math.pi*v.x2,math.pi*v.y2,math.pi*v.z2)))
		table.insert(models,model)
	end
	return models
end

function functions:ValueToString(val)
	local suffixes = {'','K','M','B','T','q','Q'} --endings
	for i=1, #suffixes do
		if val < 10^(i*3) then
			return math.floor(val/((10^((i-1)*3))/100))/(100)..suffixes[i]
		end
	end
	return val
end

return functions

2 comments

0

Edited for code block. Explain your question! https://scriptinghelpers.org/help/how-post-good-questions-answers

0

it has no red line :/