How to make plank align to wall rotation

I’m trying to make a plank building system like the game “GEF”. It’s working perfectly, but if you place planks on a wall it rotates wrong.

Video of what happens:

Code: (Server code isn’t included nor needed.)

local tool = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local plankproperties = {
	XSize = 1.5,
	YSize = 0.3,
	Material = Enum.Material.WoodPlanks,
	Color = Color3.fromRGB(124, 92, 70),
	MaxLength = 15,
	MinLenth = 1.5
}

local debouncetime = 0

local deb = false

tool.Activated:Connect(function()
	if deb == true then return end
	deb = true

	local firstpos = mouse.Hit.Position-mouse.UnitRay.Direction*plankproperties.YSize

	local plank = Instance.new("Part")
	plank.Parent = workspace
	plank.Anchored = true
	plank.CanCollide = false
	plank.CanQuery = false
	plank.Material = Enum.Material.Neon
	plank.Transparency = 0.7

	local notice = script.Notice:Clone()
	notice.Parent = plank

	local secondclick = false

	local placable = true

	task.spawn(function()
		while secondclick == false do
			local pos = firstpos
			local pos1 = mouse.Hit.Position-mouse.UnitRay.Direction*plankproperties.YSize

			local Distance = (pos-pos1).Magnitude
			plank.CFrame = CFrame.new(pos,pos1) * CFrame.new(0,0,-Distance/2)
			plank.Size = Vector3.new(plankproperties.XSize,plankproperties.YSize,Distance)
			if Distance > plankproperties.MaxLength then
				plank.Color = Color3.fromRGB(255, 0, 0)
				if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "Too Long!" end
				placable = false
			elseif Distance < plankproperties.MinLenth then
				plank.Color = Color3.fromRGB(255, 0, 0)
				if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "Too Short!" end
				placable = false
			else
				plank.Color = Color3.fromRGB(0, 255, 0)
				if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "" end
				placable = true
			end
			wait()
		end
	end)
	local equipped = true
	tool.Unequipped:Connect(function()
		plank:Destroy()
		equipped = false
		return
	end)

	tool.Deactivated:Wait()
	secondclick = true
	if equipped == true then
		if placable == true then
			script.PlaceSound:Play()
			script.Parent.Place:FireServer(plank.Size,plank.CFrame,plankproperties)
			plank:Destroy()
			task.wait(debouncetime)
			secondclick = false
			deb = false
		else
			plank:Destroy()
			task.wait(debouncetime)
			secondclick = false
			deb = false
		end
	end
end)

Please help me out the system is almost perfect!

3 Likes

can you resend the video?, idk why it keeps loading for me

Ok [CHARACTERCHARACTER]

1 Like

the vid didnt work also, but i think u r gonna make the plate copy the wall rotation?

1 Like

try this :

local UIS = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local tool = script.Parent
local plr = game.Players.LocalPlayer

local mouse = plr:GetMouse()

local plankproperties = {
	XSize = 1.5,
	YSize = 0.3,
	Material = Enum.Material.WoodPlanks,
	Color = Color3.fromRGB(124, 92, 70),
	MaxLength = 15,
	MinLenth = 1.5
}

local debouncetime = 0

local deb = false

tool.Activated:Connect(function()
	if deb == true then return end
	deb = true

	local UnitRAY = mouse.UnitRay
	local firstpos = mouse.Hit.Position-UnitRAY.Direction*plankproperties.YSize

	local plank = Instance.new("Part")
	plank.Parent = workspace
	plank.Anchored = true
	plank.CanCollide = false
	plank.CanQuery = false
	plank.Material = Enum.Material.Neon
	plank.Transparency = 0.7
	
	local notice = script.Notice:Clone()
	notice.Parent = plank

	local secondclick = false

	local placable = true

	task.spawn(function()
		while secondclick == false do
			local pos = firstpos
			UnitRAY = mouse.UnitRay
			local result = workspace:Raycast(UnitRAY.Origin, UnitRAY.Direction * 10000)
			if result then
				warn("Cast")
				local orientation = CFrame.new(Vector3.zero, result.Normal) * CFrame.Angles(math.rad(90),math.rad(90),0)
				local pos1 = mouse.Hit.Position-mouse.UnitRay.Direction*plankproperties.YSize

				local Distance = (pos-pos1).Magnitude
				plank.CFrame = CFrame.new(pos,pos1) * CFrame.new(0,0,-Distance/2) * orientation.Rotation
				plank.Size = Vector3.new(plankproperties.XSize,plankproperties.YSize,Distance)
				if Distance > plankproperties.MaxLength then
					plank.Color = Color3.fromRGB(255, 0, 0)
					if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "Too Long!" end
					placable = false
				elseif Distance < plankproperties.MinLenth then
					plank.Color = Color3.fromRGB(255, 0, 0)
					if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "Too Short!" end
					placable = false
				else
					plank.Color = Color3.fromRGB(0, 255, 0)
					if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "" end
					placable = true
				end
			else
				
				local pos1 = mouse.Hit.Position-mouse.UnitRay.Direction*plankproperties.YSize

				local Distance = (pos-pos1).Magnitude
				plank.CFrame = CFrame.new(pos,pos1) * CFrame.new(0,0,-Distance/2)
				
				plank.Size = Vector3.new(plankproperties.XSize,plankproperties.YSize,Distance)
				plank.Color = Color3.fromRGB(0, 255, 0)
				if notice:FindFirstChild("TextLabel") then notice.TextLabel.Text = "" end
				placable = true
			end
			task.wait()
		end
	end)
	local equipped = true
	tool.Unequipped:Connect(function()
		plank:Destroy()
		equipped = false
		return
	end)

	tool.Deactivated:Wait()
	secondclick = true
	if equipped == true then
		if placable == true then
			script.PlaceSound:Play()
			script.Parent.Place:FireServer(plank.Size,plank.CFrame,plankproperties)
			plank:Destroy()
			task.wait(debouncetime)
			secondclick = false
			deb = false
		else
			plank:Destroy()
			task.wait(debouncetime)
			secondclick = false
			deb = false
		end
	end
end)
1 Like

Sorry for not responding. I was away from home.

1 Like