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)
You can utilize the math function min
and max
here. These functions will return to you either the maximum or minimum of the variant number of arguments given to it.
But we don't want the minimum value or the maximum value, we want a value "clamped" between the two. With a little logic, we get this:
local function clamp(n, low, high) return math.min(math.max(n, low), high) end
However, if the high and low values are supplied the wrong way, this would give the wrong answer. You can do a little check to fix this issue, however it is not necessary if it is used correctly.
local function clamp(n, a, b) local temp = a a, b = math.min(a, b), math.max(temp, b) return math.min(math.max(n, a), b) end
You can use this function to return to you your x and z coordinates clamped between the bounds you want.
-- Assuming function clamp is defined local highBounds = Vector3.new(100, 0, 100) local lowBounds = Vector3.new(-100, 0, -100) local part = game.Workspace.Part -- Example object local x = clamp(part.Position.X, lowBounds.X, highBounds.X) local y = clamp(part.Position.Y, lowBounds.Y, highBounds.Y) local z = clamp(part.Position.Z, lowBounds.Z, highBounds.Z) part.Position = Vector3.new(x, y, z)
So if desired Part position was Vector3.new(132, -32, -92) then the part would be positioned at Vector3.new(100, 0, -92).