How do i get the mouse position

I want to achieve a placement system that runs every 4 studs.

How do i make it show up the grid thats 4x studs in x-z and make it also place it in the grid

1 Like

I think I get what you want, and the best way for me to explain it is through code, please do try this and tell me if it helps.

local script:

-- local
local PosX, PosY, PosZ
local Grid = 4 -- 4 studs snap
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Part = game.ReplicatedStorage.Part

local function Snap(posy)
    PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
    PosY = 0
    PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid -- Snaps
end

local function Move()
     Part.Parent = game.Workspace
     mouse.TargetFilter = Part
     Snap()
     Part:SetPrimaryPartCFrame(CFrame.new(PosX, mouse.Hit.p.Y + .5, PosZ))
end

mouse.Move:Connect(Move)

mouse.Button1Down:Connect(function()
    local newpart = Part:Clone()
    newpart.Parent = game.Workspace
    newpart:SetPrimaryPartCFrame(Part.Base.CFrame)
end)
3 Likes

Im getting 2 errors, one when moving and the other one when left clicking. I put it in a local script startergui

  19:45:22.614  Stack Begin  -  Studio
  19:45:22.614  Script 'Players.clarijs221.PlayerGui.LocalScript', Line 19 - function Move  

^ error when moving mouse

Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.  -  Client - LocalScript:27
  19:48:21.780  Script 'Players.clarijs221.PlayerGui.LocalScript', Line 27

^when clicking

My part does exist from multiple parts, think of it like miners haven

2 Likes

This was a bad mishap on my part, as i gave you outdated code using :SetPrimaryPartCFrame()

please try this:

-- local
local PosX, PosY, PosZ
local Grid = 4 -- 4 studs snap
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")
local Part = game.ReplicatedStorage.Part

local function Snap(posy)
    PosX = math.floor(mouse.Hit.X / Grid + 0.5) * Grid -- Snaps
    PosY = 0
    PosZ = math.floor(mouse.Hit.Z / Grid + 0.5) * Grid -- Snaps
end

local function Move()
     Part.Parent = game.Workspace
     mouse.TargetFilter = Part
     Snap()
     Part.CFrame = CFrame.new(PosX, mouse.Hit.p.Y + .5, PosZ)
end

mouse.Move:Connect(Move)

mouse.Button1Down:Connect(function()
    local newpart = Part:Clone()
    newpart.Parent = game.Workspace
    newpart.CFrame = Part.Base.CFrame
end)```
2 Likes