So I was making a grid block placement system then I ran into a problem where the blocks appear to not want to stack on some sides.
After searching for a few hours and pasting bits and pieces of other’s code into mine, I found out that FindPartOnRayWithIgnoreList works from this forum:
I used a raycast before:
local function rayCast()
local mousePosition = userInputService:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local rayCastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction*9999, rayCastParams)
return rayCastResult
end
his function I copied:
function doFakeMouseStuff()
mouse.TargetFilter = showBlock
local ignoreList = {player.Character, showBlock}
local mouseRay = mouse.UnitRay
local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.unit * 900)
target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
end
Full code if you need (yess all in local script ill add server support later)
local tool = script.Parent
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local userInputService = game:GetService("UserInputService")
local runService = game:GetService('RunService')
local blocksFolder = game:GetService('ReplicatedStorage'):WaitForChild("Blocks")
local block = blocksFolder["Dirt"]
local placedBlocksFolder = game.Workspace:WaitForChild('PlacedBlocks')
local showBlock = nil
local equipped = false
local target, pos, norm
-- function for mouse raycasting
local rayCastParams = RaycastParams.new()
rayCastParams.FilterType = Enum.RaycastFilterType.Whitelist
rayCastParams.FilterDescendantsInstances = {placedBlocksFolder, workspace.Baseplate}
local function rayCast()
local mousePosition = userInputService:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local rayCastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction*9999, rayCastParams)
return rayCastResult
end
-- function for returning snap position
local function snap(pos)
local posx = math.floor(pos.X / block.Size.X + 0.5) * block.Size.X
local posy = math.floor(pos.Y / block.Size.Y + 0.5) * block.Size.Y
local posz = math.floor(pos.Z / block.Size.Z + 0.5) * block.Size.Z
return Vector3.new(posx, posy, posz)
end
function doFakeMouseStuff()
mouse.TargetFilter = showBlock
local ignoreList = {player.Character, showBlock}
local mouseRay = mouse.UnitRay
local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.unit * 900)
target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
end
-- make the show block if equipeed
tool.Equipped:Connect(function()
equipped = true
if showBlock == nil then
showBlock = block:Clone()
showBlock.Transparency = 0.5
showBlock.CanCollide = false
showBlock.Parent= workspace
showBlock.Name = "PlayerShowBlock"
end
end)
-- destroy the show block if unequipped
tool.Unequipped:Connect(function()
equipped = false
if showBlock ~= nil then
showBlock:Destroy()
showBlock = nil
end
end)
--place the bloc
tool.Activated:Connect(function()
local newBlock = block:Clone()
newBlock.Parent = placedBlocksFolder
newBlock.CFrame = showBlock.CFrame
local rayResult = rayCast()
local pos1 = snap(pos + (norm * (block.Size*.5)))
if newBlock.CFrame ~= CFrame.new(pos1.X, pos1.Y, pos1.Z) then
newBlock.CFrame = CFrame.new(pos1.X, pos1.Y, pos1.Z)
end
end)
--move the show block
runService.Heartbeat:Connect(function(delta)
doFakeMouseStuff()
if equipped then
local rayResult = rayCast()
if rayResult and rayResult.Instance then
if showBlock then
-- Snap the blocks accordingly
local pos1 = snap(pos + (norm * (block.Size*.5)))
showBlock.CFrame = showBlock.CFrame:Lerp(CFrame.new(pos1.X, pos1.Y , pos1.Z), delta* 25)
end
end
end
end)
Can i have tips on how to write cleaner code
I dont know if this goes in the Code Review or Scripting Support.
Please tell me if this is the wrong place