User:HotThoth/WeldScript

From Legacy Roblox Wiki
Jump to navigationJump to search
local makeWeld = game.Lighting:FindFirstChild("MakeWeld")
local selectFirst = game.Lighting:FindFirstChild("WeldSelectFirst")
local selectSecond = game.Lighting:FindFirstChild("WeldSelectSecond")
local printC0 = game.Lighting:FindFirstChild("PrintC0")
local printC1 = game.Lighting:FindFirstChild("PrintC1")
local editWelds = game.Lighting:FindFirstChild("EditWelds")
local finalizeWelds = game.Lighting:FindFirstChild("FinalizeWelds")
local clearScript = game.Lighting:FindFirstChild("RemoveWeldScript")

if makeWeld == nil then
	makeWeld = Instance.new("BoolValue")
	makeWeld.Name = "MakeWeld"
	makeWeld.Value = false
	makeWeld.Parent = game.Lighting
end

if selectFirst == nil then
	selectFirst = Instance.new("BoolValue")
	selectFirst.Name = "WeldSelectFirst"
	selectFirst.Value = false
	selectFirst.Parent = game.Lighting
end

if selectSecond == nil then
	selectSecond = Instance.new("BoolValue")
	selectSecond.Name = "WeldSelectSecond"
	selectSecond.Value = false
	selectSecond.Parent = game.Lighting
end


if printC0 == nil then
	printC0 = Instance.new("BoolValue")
	printC0.Name = "PrintC0"
	printC0.Value = false
	printC0.Parent = game.Lighting
end

if printC1 == nil then
	printC1 = Instance.new("BoolValue")
	printC1.Name = "PrintC1"
	printC1.Value = false
	printC1.Parent = game.Lighting
end

if editWelds == nil then
	editWelds = Instance.new("BoolValue")
	editWelds.Name = "EditWelds"
	editWelds.Value = false
	editWelds.Parent = game.Lighting
end

if finalizeWelds == nil then
	finalizeWelds = Instance.new("BoolValue")
	finalizeWelds.Name = "FinalizeWelds"
	finalizeWelds.Value = false
	finalizeWelds.Parent = game.Lighting
end

if clearScript == nil then
	clearScript = Instance.new("BoolValue")
	clearScript.Name = "RemoveWeldScript"
	clearScript.Value = false
	clearScript.Parent = game.Lighting
end


function waitUntilOnePartSelected()
	local failSauce = true
	selection = game.Selection:Get()
	while (failSauce) do
		while (#selection ~= 1) do selection = game.Selection:Get() wait() end -- stall

		if selection[1].className ~= "Part" and selection[1].className ~= "TrussPart" and selection[1].className ~= "WedgePart" then
			print("Selected object is still not of type Part!  Try harder.")
			game.Selection:Set({})
		else failSauce = false end
	end
	return selection[1]
end

function checkIfOneWeldSelected()
	selection = game.Selection:Get()
	if #selection ~= 1 then
		print("You must select only a single weld!")
		return nil
	end
	if selection[1].className ~= "Weld" then
		print("That is not a weld you have selected.")
		return nil
	end

	return selection[1]
end


function onMakeWeld(property)
	if makeWeld.Value == false then return
	else makeWeld.Value = false end

	selection = game.Selection:Get()
	if #selection > 2 or #selection < 1 then
		print("Only 2 parts may be selected!")
		return
	end
	if selection[1].className ~= "Part" and selection[1].className ~= "TrussPart" and selection[1].className ~= "WedgePart" then
		print("First thing selected is not of type Part!  Bad you!")
		return
	end
	if selection[2].className ~= "Part" and selection[2].className ~= "TrussPart" and selection[2].className ~= "WedgePart" then
		print("Second thing selected is not of type Part!  Dislike.")
		return
	end

	-- if we get here, we know we have two parts selected...  time to bring out the spotwelders :D
	newWeld = Instance.new("Weld")
	newWeld.Part0 = selection[1]
	newWeld.Part1 = selection[2]
	newWeld.Name = selection[1].Name .. "#" .. selection[2].Name .. "#" .. "Weld"
	newWeld.C1 = newWeld.Part1.CFrame:inverse() * newWeld.Part0.CFrame

	print("Weld created...  where would you like to place it? [Select ONE part now]")

	newWeldBase = waitUntilOnePartSelected()

	newWeld.Parent = selection[1]
end


function onSelectFirst(property)
	if selectFirst.Value == false then return
	else selectFirst.Value = false end

	currWeld = checkIfOneWeldSelected()

	if currWeld == nil then return end

	game.Selection:Set({currWeld.Part0})
end

function onSelectSecond(property)
	if selectSecond.Value == false then return
	else selectSecond.Value = false end

	currWeld = checkIfOneWeldSelected()

	if currWeld == nil then return end	

	game.Selection:Set({currWeld.Part1})
end


function onPrintC0(property)
	if printC0.Value == false then return
	else printC0.Value = false end

	currWeld = checkIfOneWeldSelected()

	if currWeld == nil then return end

	print(currWeld.C0)
end

function onPrintC1(property)
	if printC1.Value == false then return
	else printC1.Value = false end

	currWeld = checkIfOneWeldSelected()

	if currWeld == nil then return end

	print(currWeld.C1)
end

local weldsToPreserve = {}
local oldParents = {}
function onEditWelds(property)
	if editWelds.Value == false then return
	else editWelds.Value = false end

	selected = game.Selection:Get()
	
	-- make sure they're all welds
	phailThis = false
	for i = 1, #selected do
		if selected[i].className ~= "Weld" then	phailThis = true end
	end

	if phailThis then
		print("One of the selected objects is not a weld; please select welds to preserve only")
		return
	end

	weldsToPreserve = {}
	oldParents = {}
	for i = 1, #selected do
		table.insert(weldsToPreserve, selected[i])
		table.insert(oldParents, selected[i].Parent)
	end
	print("Welds will be preserved upon finalizeWelds call [unless editWelds is called again first].")
end

function onFinalizeWelds(property)
	if finalizeWelds.Value == false then return
	else finalizeWelds.Value = false end

	for i = 1, #weldsToPreserve do
		tempWeld = weldsToPreserve[i]
		tempWeld.C1 = tempWeld.Part1.CFrame:inverse() * tempWeld.Part0.CFrame
		tempWeld.Parent = oldParents[i]
	end

	print(#weldsToPreserve, " welds preserved")
end


function onClearScript(property)
	if clearScript.Value == false then return
	else clearScript.Value = false end

	makeWeld:remove()
	selectFirst:remove()
	selectSecond:remove()
	printC0:remove()
	printC1:remove()
	editWelds:remove()
	finalizeWelds:remove()
	clearScript:remove()
end


makeWeld.Changed:connect(onMakeWeld)
selectFirst.Changed:connect(onSelectFirst)
selectSecond.Changed:connect(onSelectSecond)
printC0.Changed:connect(onPrintC0)
printC1.Changed:connect(onPrintC1)
editWelds.Changed:connect(onEditWelds)
finalizeWelds.Changed:connect(onFinalizeWelds)
clearScript.Changed:connect(onClearScript)