Hello!
I am currently trying to make a placement system where you can select an object from a GUI that you want to place, and it will be appear wherever your mouse clicks. The object however spawns halfway into the ground.
image
It won’t move from that spot no matter where your mouse clicks. You also can’t spawn another part as it will just overlap the past instance.
Client script:
Server script:
And yes, I am aware that some things may be deprecated, as this was from an old tutorial. I can change things if need be, but I would prefer to keep it as is. Also, let me know if I need to explain anything better.
If you think you know what went wrong, please let me know. Thank you and have wonderful day!
NOTICE: this post was posted in external text box by the administrator team
I am currently trying to make a placement system where you can select an object from a GUI that you want to place, and it will be appear wherever your mouse clicks. The object however spawns halfway into the ground.
image
It won’t move from that spot no matter where your mouse clicks. You also can’t spawn another part as it will just overlap the past instance.
Client script:
Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local Character = Player.Character or Player.Character:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local yBuildingOffset = 5
local MaxPlacingDistance = 50
local rKeyIsPressed = false
local PlacingStructure = false
for _, structureButton in pairs(StructureFrame:GetChildren()) do
if structureButton:IsA("TextButton") then
structureButton.MouseButton1Up:Connect(function()
StructureFrame.Visible = false
local yOrientation = 0
local GoodToPlace = false
local PlacedStructure
if PlacingStructure == false then
PlacingStructure = true
local ClientStructure = Structures:FindFirstChild(structureButton.Name):Clone()
ClientStructure.BrickColor = BrickColor.new("Forest green")
ClientStructure.Material = "Neon"
ClientStructure.CanCollide = false
ClientStructure.Parent = game.Workspace
local StartingCFrame = CFrame.new(0, -2, -15)
ClientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(StartingCFrame)
RunService.RenderStepped:Connect(function()
local MouseRay = Mouse.UnitRay
local CastRay = Ray.new(MouseRay.Origin, MouseRay.Direction * 1000)
local IgnoreList = {ClientStructure, Character}
local hit, position = workspace:FindPartOnRayWithIgnoreList(CastRay, IgnoreList)
if hit and (HumanoidRootPart.Position - ClientStructure.Position).Magnitude < MaxPlacingDistance then --and (hit:IsA("Terrain") or hit.Name:lower() == "terrain")
GoodToPlace = true
ClientStructure.BrickColor = BrickColor.new("Forest green")
else
GoodToPlace = false
ClientStructure.BrickColor = BrickColor.new("Crimson")
end
local NewAnglesCFrame = CFrame.Angles(0, math.rad(yOrientation), 0)
local NewCFrame = CFrame.new(position.X, position.Y + yBuildingOffset, position.Z)
ClientStructure.CFrame = NewCFrame * NewAnglesCFrame
end)
UserInputService.InputBegan:Connect(function(input) -- GPE
if input.KeyCode == Enum.KeyCode.R then -- and not GPE then
rKeyIsPressed = true
local RotationSpeed = 5
while rKeyIsPressed do
task.wait()
if PlacingStructure == true then
yOrientation = yOrientation + RotationSpeed
end
end
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
rKeyIsPressed = false
end
end)
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if PlacingStructure == true then
if GoodToPlace == true then
local StructureCFrame = ClientStructure.CFrame
PlacedStructure = PlaceStructure:InvokeServer(ClientStructure.Name, StartingCFrame)
if PlacedStructure == true then
PlacingStructure = false
ClientStructure:Destroy()
StructureFrame.Visible = true
end
end
end
end
end)
end
end)
end
end
Server script:
Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
local Structures = ReplicatedStorage:WaitForChild("Structures")
PlaceStructure.OnServerInvoke = function(Player, StructureName, StructureCFrame)
local Crafted
local RealStructure = Structures:FindFirstChild(StructureName):Clone()
if RealStructure then
RealStructure.CFrame = StructureCFrame
RealStructure.Parent = game.Workspace
Crafted = true
else
Crafted = false
end
return Crafted
end
And yes, I am aware that some things may be deprecated, as this was from an old tutorial. I can change things if need be, but I would prefer to keep it as is. Also, let me know if I need to explain anything better.
If you think you know what went wrong, please let me know. Thank you and have wonderful day!
FrizzyDino wrote:This Post Was Created By Me
NOTICE: this post was posted in external text box by the administrator team