Placement System Issue

I’ve made a Placement System for my game. The thing I am trying to achieve has more or less gone well, although I’m having a slight issue, once the parts are put into the world they float just above the terrain. The X and Z coordinates are great, but the Y is the issue. robloxapp-20200803-2257011.wmv (2.4 MB) ~ Here is a video of the bug during the game, if you cannot see properly, the part is under 1 Stud from the floor. I have tried defining the Y Coordinate I would like it to be at which is (5.5) I have also tried to change and experiment with the code, which creates no difference to the error. Here is the script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local IsPlacing = false
local HasPlaced = false

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local Model = game.ReplicatedStorage.Structures.RoundFloorTile:Clone()
Model.Parent = workspace

script.Parent.MouseButton1Click:Connect(function()

IsPlacing = true

if IsPlacing == true then
	
	script.Parent.Parent.Parent.Parent.Parent.Enabled = false
	script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = true
	
end

Mouse.Move:Connect(function()
	
	if IsPlacing == true then
		
		mouse.KeyDown:connect(function(key)
			
			if key == "x" then
				
				Model:Destroy()
				
				script.Parent.Parent.Parent.Parent.Parent.Enabled = true
				script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = false
				script.Cooldown.Disabled = false
				script.Disabled = true
				
			end
			
			if key == "e" then
				
				script.Parent.Parent.Parent.Parent.Parent.Enabled = true
				script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = false
				script.Cooldown.Disabled = false
				script.Disabled = true
				
			end
			
		end)
		
		local PosX = Mouse.Hit.X
		local PosY = Mouse.Hit.Y
		local PosZ = Mouse.Hit.Z

		Model:MoveTo(Vector3.new(PosX, PosY, PosZ))

		local function Snap()
	
			PosX = Mouse.Hit.X
			PosY = Mouse.Hit.Y
			PosZ = Mouse.Hit.Z
	
		end

		local function Movement()
	
			Mouse.TargetFilter = Model
			Snap()
	
			Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
	
		end
		
	end

end)

end)

(The script is in StarterGui under a Button which triggers the script.)

If anyone could help me with this issue that would be great.

1 Like

It’s because you are using moveTo brings everything on top, if you set in something, I would try setting the Model:SetPrimaryPartCFrame instead

I changed this, but I’m now getting an output error: Unable to cast “Vector3 to CoordinateFrame”. Also the Part being placed no longer follows the mouse. Everytime the mouse is moved that output is given.

Well the error is telling you what your doing wrong, lol. It’s because your trying to set a vector3 position, just change it to CFrame.new

Did that too. Then I started getting this output; “Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists” So I set the PrimaryPart of the Model to the only part that is in there and then the Part had an incorrect Rotation. It was Rotated at 90 Degrees.

Huh, CFrame | Documentation - Roblox Creator Hub look at this it’ll help with the rotations where you can set them how you like, but also when working with models use weldConstraints for all your parts to the primaryPart so all the parts move with the primaryPart, I’m not too good with welds tho so I might not be too helpful

Oh! Have you set the models primary part, lol

I did, although it has no effect on the issue.

Change your code to this.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local IsPlacing = false
local HasPlaced = false

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local Model = game.ReplicatedStorage.Structures.RoundFloorTile:Clone()
Model.PrimaryPart = Model[game.ReplicatedStorage.Structures.RoundFloorTile.PrimaryPart.Name]
Model.Parent = workspace

script.Parent.MouseButton1Click:Connect(function()

IsPlacing = true

if IsPlacing == true then
	
	script.Parent.Parent.Parent.Parent.Parent.Enabled = false
	script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = true
	
end

Mouse.Move:Connect(function()
	
	if IsPlacing == true then
		
		mouse.KeyDown:connect(function(key)
			
			if key == "x" then
				
				Model:Destroy()
				
				script.Parent.Parent.Parent.Parent.Parent.Enabled = true
				script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = false
				script.Cooldown.Disabled = false
				script.Disabled = true
				
			end
			
			if key == "e" then
				
				script.Parent.Parent.Parent.Parent.Parent.Enabled = true
				script.Parent.Parent.Parent.Parent.Parent.Parent.BuildConfig.Enabled = false
				script.Cooldown.Disabled = false
				script.Disabled = true
				
			end
			
		end)
		
		local PosX = Mouse.Hit.X
		local PosY = Mouse.Hit.Y
		local PosZ = Mouse.Hit.Z

		Model:MoveTo(Vector3.new(PosX, PosY, PosZ))

		local function Snap()
	
			PosX = Mouse.Hit.X
			PosY = Mouse.Hit.Y
			PosZ = Mouse.Hit.Z
	
		end

		local function Movement()
	
			Mouse.TargetFilter = Model
			Snap()
	
			Model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
	
		end
		
	end

end)

If you can, please change stuff like script.Parent.Parent.Parent.Parent into something like script:FindFirstAncestor("Parent"). This makes it a lot more readable. Of course, doing stuff like script.Parent.Parent.Parent.Parent isn’t a bad thing, though it’s just for good scripting habits + it’s easier to type down.

Other than that, have you tried subtracting the Y axis of the part by 1? Considering how its only a stud above the ground, you could probably just do something like Part.Position = Part.Position - Vector3.new(0,1,0) .

I changed the code, although I’m now getting the following output error; 00:10:28.159 - Players.TheWildFire928.PlayerGui.UserUI.BuildInterface.FrmTitle.SelectionFrm.RoundFloorTile.BuildSystem:10: invalid argument #2 (string expected, got Instance)

Thanks for the feedback, I’ll be sure to start using that in future work. The issue with that Solution would be that the part is always under a stud but its always different.

Have you tried raycasting to place parts instead? It sounds inefficient, so use it at your own risk lol

I’ve never used raycasting before so I would have to learn how to use it. Although I will definitely try it out if all else fails.

From what I can find, there is a post similar to this which can be found here. The only difference is that it incorporates snapping (though you can just tinker with the positioning stuff, so it probably shouldn’t be an issue).

Okay, I’ll be sure to check it out, I’ll also try and have a look at some of the solutions In that post maybe the error is of a similar origin.

1 Like