I need help in creating a prediction block system like in minecraft

  1. What do you want to achieve? Keep it simple and clear!
    I want create a block system like skywars and minecraft. I already have a block system and all i need is a prediction system that detects if the mouse is in front of a block.

  2. What is the issue? Include screenshots / videos if possible!
    Video of issue:

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for solutions and searched them up and found two forum posts. They were too hard to understand.

local camera = workspace.CurrentCamera

local players = game:GetService("Players")
local reps = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local player = players.LocalPlayer
local tool = script.Parent
local parts = reps:WaitForChild("Parts")
local outline = parts.Outline:clone()
local block = parts.Block
local mouse = player:GetMouse()
local reach = 100
local castParams = RaycastParams.new()
local result = nil
local result2 = nil
local pos = Vector3.new()
local equipped = false

local char = player.Character

while not char do
	char = player.Character
	wait()
end

local hrp = char:WaitForChild("HumanoidRootPart")

outline.Parent = workspace
outline.SelectionBox.Transparency = 1

local function snap(newcf)
	local x = math.round(newcf.X/4)*4
	local y = math.round(newcf.Y/4)*4
	local z = math.round(newcf.Z/4)*4
	return Vector3.new(x,y,z)
end

tool.Equipped:Connect(function()
	equipped = true
	outline.SelectionBox.Transparency = 0
end)

tool.Unequipped:Connect(function()
	equipped = false
	outline.SelectionBox.Transparency = 1
end)

mouse.Button1Down:Connect(function()
	if equipped then
		if result then
			local newBlock = block:Clone()
			newBlock.Position = Vector3.new(math.round(pos.x/4)*4,math.round(pos.y/4)*4,math.round(pos.z/4)*4)
			newBlock.Parent = workspace
		end
	end
end)

rs.RenderStepped:Connect(function()
	if equipped then
		local unitRay = mouse.UnitRay
		castParams.FilterDescendantsInstances = {player.Character,outline}
		result = workspace:Raycast(unitRay.Origin,unitRay.Direction*reach,castParams)
		if result then
			pos = result.Position+result.Normal*2
			outline.Position = Vector3.new(math.round(pos.x/4)*4,math.round(pos.y/4)*4,math.round(pos.z/4)*4)
		elseif not result then
			local camray = camera:ScreenPointToRay(mouse.X,mouse.Y)
			local ray2 = Ray.new(camray.Origin,camray.Direction*1000)
			local part1,pos1 = workspace:FindPartOnRayWithIgnoreList(ray2,{player.Character,outline},false,true)
			local lv = CFrame.new(pos1*-1).LookVector*4
			outline.Position = snap(lv)
		end
	end
end)
2 Likes

uh oh, sorry i don’t think i got that skills to do it, but please be nice to rat

ok i will be nice to rat but please help i cant do it and its so hard. i saw other people get replied to but not me

ight i’ll try my best to figure this out, it looks like a block placing system on a grid, try searching for some tutorials

here are some stuff,
maybe they can be helpful

1 Like

i already have a building system like this but i want it so you can build fowards like in minecraft

mind sending me the tool, i’ll try my best to make it work

ok here Block - Roblox

yeah i can tell that you’re sanity is through the roof on figuring out how to make a prediction block placing system, i don’t even know make it

You can compare Mouse X and Z position with a block, so Y will be depending on Character’s Y.

I dont know how i can do that and you can build fowards if you arent standing on the part

Ok then ask people with my message, they will help you.

I have the same problem and have posted something about it.

Bump! kinda not really

I have it working from the left and right sides but not the front and back. It’s also not really accurate and doesn’t detect the back, left, and bottom normals.

my code:

local camera = workspace.CurrentCamera

local players = game:GetService("Players")
local reps = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local player = players.LocalPlayer
local tool = script.Parent
local parts = reps:WaitForChild("Parts")
local outline = parts.Outline:clone()
local block = parts.Block
local lastblock = nil
local mouse = player:GetMouse()
local reach = 100
local castParams = RaycastParams.new()
local result = nil
local result2 = nil
local pos = Vector3.new()
local equipped = false
local deebug = reps:WaitForChild("Debug")
local r = deebug.Ray
local m = deebug.Mouse
r.Parent = workspace
m.Parent = workspace

local char = player.Character

while not char do
	char = player.Character
	wait()
end

local hrp = char:WaitForChild("HumanoidRootPart")

outline.Parent = workspace
outline.SelectionBox.Transparency = 1

local function snap(v3)
	return Vector3.new(math.round(v3.x/4)*4,math.round(v3.y/4)*4,math.round(v3.z/4)*4)
end

local function debugg(rp,mp)
	r.Position = rp
	m.Position = mp
end

tool.Equipped:Connect(function()
	equipped = true
	outline.SelectionBox.Transparency = 0
end)

tool.Unequipped:Connect(function()
	equipped = false
	outline.SelectionBox.Transparency = 1
end)

mouse.Button1Down:Connect(function()
	if equipped then
		if result then
			local newBlock = block:Clone()
			newBlock.Position = snap(pos)
			newBlock.Parent = workspace
		end
	end
end)

rs.RenderStepped:Connect(function()
	if equipped then
		local unitRay = mouse.UnitRay
		castParams.FilterDescendantsInstances = {player.Character,outline,r,m}
		result = workspace:Raycast(unitRay.Origin,unitRay.Direction*reach,castParams)
		if result then
			local tsf = mouse.TargetSurface
			pos = result.Position+result.Normal*2
			outline.Position = snap(pos)
			mouse.TargetFilter = outline
			lastblock = mouse.Target
		elseif not result then
			local tsf = mouse.TargetSurface
			local dir = Vector3.FromNormalId(tsf)
			outline.Position = snap(lastblock.Position+(dir*(Vector3.new(unitRay.Direction.X*2,1,1)))*4)
		end
	end
end)