This script is able to move, rotate, and place a model. The item gets placed on a 5x5 block grid.
QUESTION: How will I be able to keep the item within the grid (There are limitations where the item can go); It needs to stay on the grid.
I need the proper way of doing this, I've already tried to make it move only if the mouse's target was the grid; but it was fairly inaccurate as it was slightly still going off the grid.
Localscript:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local PlacedPart = false
local Selected = true
local Rotated = false
local Rotate = 1.57
local Rotation
local yAxis = 0.6
local Enabled = true
local Item = game.Workspace.Model
Mouse.Button1Down:connect(function()
if Enabled and not PlacedPart then
if Item ~= nil then
ClearSelection()
end
end
end)
--[[
function Check()
end
--]]
Mouse.Button2Down:connect(function()
if not Rotated or Rotation == 0 then
Rotation = 1
Rotated = true
Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
elseif Rotation == 1 then
Rotation = 2
Rotated = true
Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
elseif Rotation == 2 then
Rotation = 3
Rotated = true
Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
elseif Rotation == 3 then
Rotation = 0
Item:SetPrimaryPartCFrame(CFrame.new(Item.PrimaryPart.Position) * CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
end
end)
function ClearSelection()
PlacedPart = false
Selected = false
Rotated = false
Item = nil
end
Mouse.Move:connect(function()
if Selected and not PlacedPart and Enabled then
if Item ~= nil then
if Rotated then -- Mouse.Hit.p.X - Mouse.Hit.p.X%25, Spawn.Position.Y, Mouse.Hit.p.Z - Mouse.Hit.p.Z%25
Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5)*CFrame.fromEulerAnglesXYZ(0,Rotate*Rotation,0))
else
Item:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X - Mouse.Hit.p.X%5, yAxis, Mouse.Hit.p.Z - Mouse.Hit.p.Z%5))
end
end
end
end)

1 comment
I better way to rotate/move would be appreciated also!