So I made a build system and when you move the mouse the x and z fight making the part move diagonal and out of grid. PLEASE HELP!!!! No one has and I've asked for a WEEK!
local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local PlaceObject = game.ReplicatedStorage.BuildEvents.PlaceObject
local GivenPlot = game.ReplicatedStorage.BuildEvents.GivenPlot
local PlaceObject = game.ReplicatedStorage.BuildEvents.PlaceObject
local GridSize = 2
local posX
local posY
local posZ
local YPos = Vector3.new(0, 75, 0)
local Rot = 0
local Object
local Plot
local PlotO
local ButtonObject = script.Parent.ScrollingFrame.Soil.ImageButton
local CanStart = true
IsPlacing = nil
local CanPlace = nil
GivenPlot.OnClientEvent:Connect(function(plt)
Plot = plt.Name
PlotO = plt.Orientation
PlotX = PlotO.X
PlotY = PlotO.Y
PlotZ = PlotO.Z
end)
local function Placement()
if IsPlacing and CanPlace then
PlaceObject:FireServer(posX, posY, posZ, Rot, Object.Name, PlotX, PlotY, PlotZ)
IsPlacing = false
CanPlace = false
CanStart = true
Object:Destroy()
end
end
local function Snap()
posX = math.floor(Mouse.Hit.X / GridSize + 0.5)* GridSize
posY = Mouse.Hit.Y
posZ = math.floor(Mouse.Hit.Z / GridSize + 0.5)* GridSize
Object.PrimaryPart.Orientation = (Vector3.new(PlotX, PlotY, PlotZ))
end
local function Movement()
if IsPlacing and CanPlace and not CanStart and Mouse.Target and Mouse.Target.Name == Plot then
Mouse.TargetFilter = Object
Object:SetPrimaryPartCFrame(Object.PrimaryPart.CFrame:Lerp(CFrame.new(posX, posY, posZ)* CFrame.Angles(0, math.rad(Rot), 0), 0.2))
Object.PrimaryPart.Orientation = (Vector3.new(PlotX, PlotY, PlotZ))
Snap()
else
return Mouse.Hit.p
end
end
local function StartPlacementSoil()
if CanStart then
Object = game:GetService("ReplicatedStorage").Soil:Clone()
Object.Parent = workspace
Mouse.Move:Connect(Movement)
IsPlacing = true
CanPlace = true
CanStart = false
Mouse.Move:Connect(Movement)
end
end
local function StartPlacement(obj)
if CanStart then
Object = game.ReplicatedStorage:FindFirstChild(obj):Clone()
Object.Parent = workspace
IsPlacing = true
CanPlace = true
CanStart = false
end
end
local function Rotation(input, GPE)
if IsPlacing and CanPlace and not CanStart and input.KeyCode == Enum.KeyCode.R then
Rot = Rot + 90
Object:SetPrimaryPartCFrame(Object.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
end
end
Mouse.Button1Down:Connect(Placement)
ButtonObject.MouseButton1Click:Connect(function()
StartPlacement("Soil")
end)
RunService:BindToRenderStep("Input", Enum.RenderPriority.Input.Value, Movement)
UIS.InputBegan:Connect(Rotation)
