Minimap does not align when chunk changes

i created a minimap by following Suphi Kaner’s video ( Minimap - Roblox Scripting Tutorial ), and i followed all the exact steps to fix the “jumping between chunks” bug but it doesn’t seem to work for me.

External Media
local Players = game:GetService('Players')
local UIS = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local CAS = game:GetService("ContextActionService")

local tweenInfo = TweenInfo.new(0.28, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

local Player = Players.LocalPlayer
local Mouse = game.Players.LocalPlayer:GetMouse()

local scrollConstant = 500
local scrollDistance

local offset, chunkSize, chunkHalfSize, currentX, currentZ
local InitialX, InitialY, UIInitialPos

local mousePull = "http://www.roblox.com/asset/?id=95875519701158"
local mouseDrag = "http://www.roblox.com/asset/?id=121158864833685"
local mouseHover = "http://www.roblox.com/asset/?id=120582267950180"
local mousePhone = "rbxassetid://14500233914"

local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera

local Phone = script.Parent
local Main = Phone.Main
local Border = Main.Border
local Screen = Main.Screen

local delAppFrame = Screen.DeliveryApp
local delAppVPFrame = delAppFrame.ViewportFrame

local viewportCamera = Instance.new("Camera")
viewportCamera.FieldOfView = 20
delAppVPFrame.CurrentCamera = viewportCamera

local pullButton = Main.PullOut
local deliveryIcon = Screen.DeliveryIcon

local deliveryApp = Screen.DeliveryApp

Main.Position = UDim2.new(0.8,0,1.2,0)

local Out = false
local Hovered = false
local Holding = false
local OverScreen = false
local MoveCon = nil

for i, descendant in ipairs(workspace.Map:GetDescendants()) do
	if descendant:IsA("BasePart") == false then continue end
	local clone = descendant:Clone()
	clone.Parent = delAppVPFrame
end

RunService.RenderStepped:Connect(function()
	if Holding then
		Mouse.Icon = mouseDrag
	elseif Hovered and not OverScreen then
		Mouse.Icon = mouseHover
	else
		Mouse.Icon = ""
	end
end)

local function Update()
	local cx = camera.Focus.Position.X / chunkSize
	local cz = camera.Focus.Position.Z / chunkSize

	local chunkX = math.floor(cx)
	local chunkZ = math.floor(cz)

	local x = cx % 1
	local z = cz % 1

	if currentX ~= chunkX or currentZ ~= chunkZ then
		currentX, currentZ = chunkX, chunkZ
		local position = Vector3.new(chunkX * chunkSize + chunkHalfSize, 0, chunkZ * chunkSize + chunkHalfSize)
		viewportCamera.CFrame = CFrame.lookAt(position + offset, position, -Vector3.zAxis)
	end

	delAppVPFrame.Position = UDim2.new(1 - x, 0, 1 - z, 0)
end

local function SetOffset(value)
	offset = Vector3.new(0, value, 0)
	chunkSize = offset.Y * math.tan(math.rad(viewportCamera.FieldOfView) / 2)  
	chunkHalfSize = chunkSize / 2
	currentX, currentZ = math.huge, math.huge
	Update()
end

local function Drag()
	if not Holding then MoveCon:Disconnect(); return end
	local distanceMovedX = InitialX - Mouse.X
	local distanceMovedY = InitialY - Mouse.Y

	Main.Position = UIInitialPos - UDim2.new(0, distanceMovedX, 0, distanceMovedY)
end

SetOffset(725)

camera:GetPropertyChangedSignal("Focus"):Connect(function()
	Update()
end)

UIS.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		if OverScreen then
			scrollDistance = (-input.Position.Z * scrollConstant) * 0.05
			SetOffset(offset.Y + scrollDistance)
		end
	end
end)

Border.MouseEnter:Connect(function()
	if Out then
		Hovered = true
	end
end)

Border.MouseLeave:Connect(function()
	Hovered = false
end)

Screen.MouseEnter:Connect(function()
	OverScreen = true
end)

Screen.MouseLeave:Connect(function()
	OverScreen = false
end)

pullButton.MouseEnter:Connect(function()
	if not Out then
		Mouse.Icon = mousePull
	end
end)

pullButton.MouseLeave:Connect(function()
	Mouse.Icon = ""
end)

pullButton.MouseButton1Click:Connect(function()
	if not Out then
		Out = true
		TweenService:Create(Main, tweenInfo, {Position =  UDim2.fromScale(Main.Position.X.Scale, 0.6)}):Play()
	else
		TweenService:Create(Main, tweenInfo, {Position =  UDim2.fromScale(Main.Position.X.Scale, 1.2)}):Play()
		Out = false
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		Holding = Hovered and not OverScreen
		if Holding and Out then
			InitialX, InitialY = Mouse.X, Mouse.Y
			UIInitialPos = Main.Position

			MoveCon = Mouse.Move:Connect(Drag)
		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		Holding = false
	end
end)

CAS:BindAction("DisableScroll",
	function()
		return OverScreen and Enum.ContextActionResult.Sink or Enum.ContextActionResult.Pass
	end, false, Enum.UserInputType.MouseWheel)

these are the important ones

local function Update()
	local cx = camera.Focus.Position.X / chunkSize
	local cz = camera.Focus.Position.Z / chunkSize

	local chunkX = math.floor(cx)
	local chunkZ = math.floor(cz)

	local x = cx % 1
	local z = cz % 1

	if currentX ~= chunkX or currentZ ~= chunkZ then
		currentX, currentZ = chunkX, chunkZ
		local position = Vector3.new(chunkX * chunkSize + chunkHalfSize, 0, chunkZ * chunkSize + chunkHalfSize)
		viewportCamera.CFrame = CFrame.lookAt(position + offset, position, -Vector3.zAxis)
	end

	delAppVPFrame.Position = UDim2.new(1 - x, 0, 1 - z, 0)
end

local function SetOffset(value)
	offset = Vector3.new(0, value, 0)
	chunkSize = offset.Y * math.tan(math.rad(viewportCamera.FieldOfView) / 2)  
	chunkHalfSize = chunkSize / 2
	currentX, currentZ = math.huge, math.huge
	Update()
end

(im very sorry for the long script, i haven’t cleaned it up yet)

thanks in advance.

1 Like

If line 67-92 are very important, please send them as a seperate code snippet :slight_smile:

Thanks.

2 Likes