Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My own Grid thing is a little odd how do i fix it?

Asked by 5 years ago

Im Trying to make a custom grid placement system without the use of the tutorials that i dont understand and to see if I can simplify it so,

--The script is small

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local Grid = game.Workspace.Grid
local GridSize = .5

local Marker = game.Workspace.Marker

local PosX = Marker.CFrame.X
local PosY = Marker.CFrame.Y
local PosZ = Marker.CFrame.Z

mouse.Move:Connect(function()
    if mouse.Target == Grid then
        mouse.TargetFilter = game.Workspace.Marker
Marker.Position = Vector3.new(math.floor(mouse.Hit.X / GridSize + .05), mouse.Hit.Y, math.floor(mouse.Hit.Z / GridSize + .05))
    end
end)

So Here is the Grid issue The marker goes off the actual grid how does one prevent that

2
I think you've messed up the math.floor(mouse.Hit.X / GridSize + .05) and math.floor(mouse.Hit.Z / GridSize + .05) lines. Test with this instead: Vector3.new(math.floor(mouse.Hit.X - GridSize), mouse.Hit.Y, math.floor(mouse.Hit.Z - GridSize)) Ankur_007 290 — 5y
0
pls dont flag for spam but hi ankur <3 its ur boi Psudar 882 — 5y

1 answer

Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago
Edited 5 years ago

The problem with the marker being far away from the mouse is simple! Try the script below. Now, if your trying to make the marker stay in the plot/grid, then you should either use region3 or detect when the object is at the edge. I could not figure out which problem was yours based off the video given. Anyway, try the code below to fix the one problem.

local runService = game:GetService("RunService")



local player = game.Players.LocalPlayer

local mouse = player:GetMouse()



local marker = game.Workspace.Marker
local grid = game.Workspace.Grid

local gridSize = 0.5
local yPos = 2

local positions = {}

local function CalcMousePos()
    -- I am using an array of positions so I can easily access them later

    positions = {
        ["x"] = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize;
        ["z"] = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
    }
end

-- Moves the marker
local function MoveMarker()
    mouse.TargetFilter = marker
    marker.CFrame = CFrame.new(positions.x, yPos, positions.z)
end

-- Updates every rendered frame

runService.RenderStepped:Connect(function()
    CalcMousePos()

    MoveMarker()
end)
0
i did figure it out but because your method did work ill accept The_Pr0fessor 595 — 5y
0
ok zblox164 531 — 5y
Ad

Answer this question