Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Moving a model within a grid?

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

0

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

1 answer

✓ Accepted answer

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.

###Usage Example:

-- 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).

6 comments

0

What would n, a, and b represent? Sorry, really new to the math portion of lua

0

Sorry, n is the value you want to clamp, a and b are your upper and lower limits

0

Need more help on this @BlackJPI

0

What are you having trouble with?

0

@BlackJPI, So the n is a value. would that be a position or something? Also, the a, and b; would the values be positions? AND lastly, how can I use this in my script?

0

Check my edit, you could use this in your script by clamping your calucluated x and z values when setting the model CFrame

This discussion is preserved as it was. New answers, comments, and votes are closed.