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

How would i do so if i click on textobject object clones?

So i have a placement system, but it's like you press "G" on your keyboard and then you get to move around one object that i choose in the script but i want so the player can choose the object by clicking on a gui but i don't really know how i would do this in this script i tried by adding a mouseButton1Click function but that didint work, i dont want so you press G to start placing the object i want so if you click on the machine textbutton then you start placing that somewhere on your plate and if you click on the wall button you can place and move the wall around

heres the script: localscript

local grid_snap = function(model, grid, index)
	
		movingmodel = model;
		
		mouse.TargetFilter = game.Workspace:WaitForChild("Container");
		
		local x, y = math.floor(mouse.Hit.X), math.floor(mouse.Hit.Z);
			local mouseposition = Vector3.new(x, grid.Position.Y, y);
			
			local magnitude, nearpoint = math.huge, nil;
			
			for _,v in pairs(settings.gridindex) do
				for i,z in pairs(v) do
					
					local position = z.position;
						local pointmagn = (position.p - mouseposition).magnitude;
						if (pointmagn < magnitude) then
							nearpoint, magnitude = z, pointmagn;
						end;
				end;
			end;
			
			if (movingmodel) then
				if (nearpoint.occupied) then
					model.PrimaryPart.Color = Color3.new(1, 48/255, 51/255);
						edit_properties(model, {Color = Color3.new(1, 48/255, 51/255), Transparency = 1;});
						edit_properties(model, {CanCollide = false;});
				else
					model.PrimaryPart.Color = Color3.new(67/255, 142/255, 82/255);
					edit_properties(model, {Color = Color3.new(67/255, 142/255, 82/255), Transparency = .4;});
					edit_properties(model, {CanCollide = false;});
				end;
			end;
			
			settings.hitspot = nearpoint;			
			
			model:SetPrimaryPartCFrame((CFrame.new(0, 0.2, 0) + nearpoint.position.p) * CFrame.Angles(0, math.rad(model.PrimaryPart.Orientation.Y), 0));
end;

local setup_index = function(grid, index)
	
	local x, y = grid.Size.X, grid.Size.Z;
		local xlength, ylength = math.floor(x / settings.gridsize), math.floor(y / settings.gridsize);
		
	for xindex = 1, xlength do
		
		index[xindex] = {};
		
		for yindex = 1, ylength do
			
			index[xindex][yindex] = {
				occupied 	= false;
				position	= CFrame.new(-((xindex - 1) * 4) + ((x / 2) - (settings.gridsize / 2)), 0,
										 -((yindex - 1) * 4) + ((y / 2) - (settings.gridsize / 2))) + grid.Position;			
				indexitem   = nil;
				};
		end;
	end;
	
	
	
end;
			
		
local grid = function(base)
	local x, y = base.Size.X, base.Size.Z;
		local xlength, ylength = math.floor(x / settings.gridsize), math.floor(y / settings.gridsize);
		
		local folder = camera:FindFirstChild("GridIndex");
		if (folder) then folder:Destroy(); end;
		
		folder = create(Instance.new("Folder"), {
				Name 	= "GridIndex";
				Parent	= camera;
			
		});
		
local tweenInfo = TweenInfo.new(
	.4,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.In,
	0,
	false,
	0
);
		
		for i = 1,(xlength + 1) do
			
			local line = create(Instance.new("LineHandleAdornment"), {
					SizeRelativeOffset	= Vector3.new(1 - (((i - 2) * 2) / xlength), 0, 1) + settings.gridoffset;
					Color3				= Color3.new(1,1,1);
					Length				= y;
					Thickness			= settings.gridthickness;
					Adornee				= base;
					Parent				= folder;
			});
			
			local tween = game:GetService("TweenService"):Create(line, tweenInfo, {Length = y});
				tween:Play();
			
			end;
		
			for v = 1,(ylength + 1) do
			
				local line = create(Instance.new("LineHandleAdornment"), {
						CFrame				= CFrame.new(0, 0, 0) * CFrame.Angles(0, math.rad(90), 0);
						SizeRelativeOffset	= Vector3.new(1, 0, 1 - (((v - 1) * 2) / ylength)) + settings.gridoffset;
						Color3				= Color3.new(1,1,1);
						Length				= x;
						Thickness			= settings.gridthickness;
						Adornee				= base;
						Parent				= folder;
				});
				
				local tween = game:GetService("TweenService"):Create(line, tweenInfo, {Length = x});
					tween:Play();
			
				end;
				

end;
	

local insert_model = function(model)
	
	local previewmodel, placed = game.Workspace["Container"].Arcade:Clone(), false;
		previewmodel.Parent = game.Workspace["Container"].Preview;
			previewmodel.Base.Transparency = 0;
		
	mouse.Button1Down:connect(function()
		if (not settings.hitspot.occupied) then
			placed = true;
		end;
	end);
	
	repeat wait()
		grid_snap(previewmodel, settings.grid, settings.gridindex)
	until (placed)
	
	local folder = camera:FindFirstChild("GridIndex");
		if (folder) then folder:Destroy(); end;
		
	local placedmodel = game.Workspace["Container"].Arcade:Clone();
		placedmodel.Parent = game.Workspace["Container"];
		
		placedmodel:SetPrimaryPartCFrame((CFrame.new(0, 0.2, 0) + settings.hitspot.position.p) * CFrame.Angles(0, math.rad(placedmodel.PrimaryPart.Orientation.Y), 0));
		
	previewmodel:Destroy()
		
	movingmodel = nil;
	
	settings.hitspot.occupied = true;
end;

game:GetService("UserInputService").InputBegan:connect(function(input)
	
				if (input.KeyCode == Enum.KeyCode.R) and (movingmodel ~= nil) then
					movingmodel:SetPrimaryPartCFrame(movingmodel.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(90), 0));
				elseif (input.KeyCode == Enum.KeyCode.G) and (movingmodel == nil) then
					
					grid(settings.grid);
					
					insert_model(models[chosenModel].Model)
				end;
	end);

setup_index(settings.grid, settings.gridindex);	

2 comments

0

This isnt the ful script but thats the placement system in the script

0

For InputBegan I suggest you include the second parameter function(input, isOnGui) so if they're typing it doesn't run when they press G it'll be a boolean value and you should have it do if not isOnGui then

1 answer

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local objectPlacing = nil
local endPlacement = true

function movement()
	while not endPlacement do
		wait()
		objectPlacing:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.p))
	end
end

--Loops through all the buttons if clicked it finds an object in Replicated storage with the same name as the text button and clones it
for i,v in ipairs(tableOfTextButtons)do
	v.InputBegan:Connect(function(key)
		if key.UserInputType == Enum.UserInputType.MouseButton1 and endPlacement then
			endPlacement = false
			objectPlacing = game.ReplicatedStorage:FindFirstChild(v.Name):clone()
			objectPlacing.Parent = plr.Character -- If it wasn't in the character, then the mouse's .Hit.p feature would register as on top of the object you're placing
			movement()
		end
	end)
end

then to stop placement just do endPlacement = true

1 comment

0

But how would i do so if you click on one of the textbuttons then you get to place around the object you clicked on

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