Greetings. I am trying to make a grid placement system for my level editor, but the frames arent staying on the correct positions. This is the code:
local CAMX,CAMY = 0,-1.5
local background = script.Parent:FindFirstChild("Background")
local floor = script.Parent:FindFirstChild("Floor")
local UIS = game:GetService("UserInputService")
local LevelFolder = script.Parent:FindFirstChild("LevelFolder")
local ItemsFolder = script:FindFirstChild("Items")
local Mouse = game.Players.LocalPlayer:GetMouse()
local Brush = nil
local gridsize = 0.04
local width,height = 32,23
local ghostblock = nil
local function GetSnappedPosOfMouse()
local posx,posy
posx = math.floor(((Mouse.X + CAMX)/32))
posy = math.floor(((Mouse.Y + CAMY)/32))
return Vector2.new(posx,posy)
end
local function UpdateGhostBlock()
if Brush == nil then if ghostblock then ghostblock:Destroy() ghostblock = nil end return nil end
if ghostblock == nil then
ghostblock = ItemsFolder:FindFirstChild(Brush):Clone()
ghostblock.Transparency = 0.3
ghostblock.Parent = script.Parent
end
local SnappedPos = GetSnappedPosOfMouse()
ghostblock.Position = UDim2.new(0,SnappedPos.X,0,SnappedPos.Y)
end
local function StepCamera()
background.Position = UDim2.new(0,0,-10.1-CAMY,0)
floor.Position = UDim2.new(0,0,-0.8-CAMY,0)
end
UIS.InputBegan:Connect(function(key, isTyping)
if not isTyping then
if key.KeyCode == Enum.KeyCode.One then
Brush = "Block"
elseif key.KeyCode == Enum.KeyCode.Zero then
Brush = nil
end
end
end)
local OldMouse = UIS:GetMouseDelta()
Mouse.Button1Down:Connect(function() M1Down = true end)
Mouse.Button1Up:Connect(function() M1Down = false end)
Mouse.Button2Down:Connect(function() M2Down = true end)
Mouse.Button2Up:Connect(function() M2Down = false end)
UIS.InputChanged:Connect(function(key)
if key.UserInputType == Enum.UserInputType.MouseMovement then
if M2Down then
local MouseDelta = UIS:GetMouseDelta()
local result = Vector2.new(MouseDelta.X - OldMouse.X, MouseDelta.Y - OldMouse.Y)
CAMX -= MouseDelta.X/100
CAMY -= MouseDelta.Y/100
end
end
end)
game:GetService("RunService").RenderStepped:Connect(UpdateGhostBlock)
game:GetService("RunService").RenderStepped:Connect(StepCamera)
Thanks in advance!
