I have a place system that just replicates and clones the decided object from replicated storage and puts it where the clients mouse is but its rotating sideways for some reason.
Remote Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage.Remotes:WaitForChild("Place")
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
RealStructure.Owner.Value = player.Name
RealStructure.ObjectName.Value = RealStructure.Name
crafted = true
else
crafted = false
end
return crafted
end
Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlaceStructure = ReplicatedStorage.Remotes:WaitForChild("Place")
local Structures = ReplicatedStorage:WaitForChild("Structures")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local StructureFrame = script.Parent.StructureFrame
local char = player.Character or player.Character:Wait()
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local yBuildingOffset = 2
local maxPlacingDistance = 50
local zKeyIsPressed = false
local placingStructure = false
local yOrientation=0
local goodTOPlace = false
local placedStructure
local clientStructure
-- Handles start of temporary brick rotation
function handleKeyInputStarted(input)
zKeyIsPressed = true
local rotationSpeed = 5
while zKeyIsPressed do
wait()
if placingStructure == true then
yOrientation = yOrientation + rotationSpeed
end
end
end
-- Handles end of temporary brick rotation
function handleKeyInputEnded(input)
if input.KeyCode == Enum.KeyCode.Z then
zKeyIsPressed = false
end
end
-- Handles construction of actual Brick when user is satisfied with placement/rotation
function handleMouseInputBegan(input)
if placingStructure == true then
if goodTOPlace == true then
local StructureCFrame = clientStructure.CFrame
placedStructure = PlaceStructure:InvokeServer(clientStructure.Name, StructureCFrame)
if placedStructure == true then
placingStructure = false
clientStructure:Destroy()
StructureFrame.Visible = true
end
end
end
end
-- Handles general input (mouse or keyboard) input
function handleInputStarted(input)
if input.KeyCode == Enum.KeyCode.Z then
handleKeyInputStarted(input)
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
handleMouseInputBegan(input)
end
end
-- Handles rendering the temporary Brick during placement
function handleRenderStepped()
local mouseRay = mouse.UnitRay
local castRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
local ignoreList = {clientStructure, char}
local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay, ignoreList)
if hit and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDistance then
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
-- Handles constructing the temporary placement brick when the create button is pressed.
function handleBrickButtonPressed(structureName)
StructureFrame.Visible = false
if placingStructure == false then
placingStructure = true
clientStructure = Structures:FindFirstChild(structureName):Clone()
clientStructure.BrickColor = BrickColor.new("Forest green")
clientStructure.Material = "Neon"
clientStructure.CanCollide = false
clientStructure.Parent = game.Workspace
local startingCFrame = CFrame.new(0, 0, 0)
clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
RunService.RenderStepped:Connect(handleRenderStepped)
end
end
-- Iterate over all Buttons in the StructureFrame and attach a mouse listener
for _, structureButton in pairs(StructureFrame:GetChildren()) do
if structureButton:IsA("TextButton") then
structureButton.MouseButton1Up:Connect(function()
handleBrickButtonPressed(structureButton.Name)
end)
end
end
UIS.InputBegan:Connect(handleInputStarted)
UIS.InputEnded:Connect(handleKeyInputEnded)
---