Rounding Vector3 coordinates

Here is my script:

wait(3)
local original = script.Parent.CFrame.Position
local event = game.ReplicatedStorage.PartReplace
local part = script.Parent


while wait(5) do
	local newposition = script.Parent.CFrame.Position
	
	if newposition ~= original then
		script.Parent.Transparency = 1
		part.CanCollide = false
		
	end
	
	
	
	
	
end

local function onEvent()
	
	
	local newposition = script.Parent.CFrame.Position
	
	
	if newposition ~= original then
		
		part.Position = Vector3.new(original)
		
		
	end
	
	
	
end

event.Event:Connect(onEvent)

So I have a part in the workspace. There is a loop that triggers every 5 seconds and checks to see if the part’s origin is equivalent to the part’s current position. If it isn’t equivalent, it makes the part transparent and turns can collide off. The problem is that the coordinates of the original position and the new position of the part are never equivalent because roblox studio has a system where it will not round and will go to the 1 millionth decimal place and more. For example:

The original position will be this: 25.999998092651367, 0.9972555041313171, -21.000499725341797
And the new position will be this: 26, 0.9950090646743774, -21.000038146972656

Is there a way I can round these values to the nearest integer and then put them back into a coordinate format?

1 Like

The best solution would be to use math.round(value) on your values. This will return the rounded number of the value, just like you need in your case.

1 Like
-if newposition ~= original then
+if not newposition:FuzzyEq(original) then
3 Likes

If you want to actually round up Vector3 values you can use the following function:

local function roundV3(v3: Vector3, precision: number?): Vector3
	local mul = 10^(precision or 0)
	local function r(x: number): number
		return math.round(x*mul)/mul
	end
	return Vector3.new(r(v3.X), r(v3.Y), r(v3.Z))
end

However you can also just check if the distance between the two vectors passes a certain threshold:

local threshold = 2 --bigger threshold = bigger rounding
if (original-newposition).Magnitude > threshold then
	print("Position changed!")
end

Try maybe using three different IntValues, one for each coordinate, and then make the part move toward the position. For example

while true do
local x = script.Parent.xCoordinate.Value
local y = script.Parent.yCoordinate.Value
local z = script.Parent.zCoordinate.Value
x = math.random(-20, 20)
y = math.random(-20, 20)
z = math.random(-20, 20)
script.Parent.Position = Vector3.new(x, y, z)
--[[ or if you want to tween it,
TweenService = game:GetService("TweenService")
TweenService:Create(TweenInfo.new(
script.Parent,
5,
Enum.EasingStyle = Sine,
Enum.EasingDirection = InOut,
0, false, 0), {Position = Vector3.new(x, y, z)
):Play()
wait(5)
end

Just note that there might be a mistake in the tweening code. I am not exactly the best coder.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.