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
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.
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.
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
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) .
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.
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).