I was trying to learn how to create player build mode. I am trying to make some scripts that can make me place some blocks by clicking.
LocalScript:
local InBuildMode = false
local UIS = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()
local ShowingPart = game.ReplicatedStorage:WaitForChild("Part")
local BuildBlock = game.ReplicatedStorage:WaitForChild("BuildBlock")
Mouse.TargetFilter = ShowingPart
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.B then
if InBuildMode == false then
InBuildMode = true
else
InBuildMode = false
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
if InBuildMode == true then
local X = math.round(Mouse.Hit.x/4) * 4
local Y = math.round(Mouse.Hit.y/4) * 4
local Z = math.round(Mouse.Hit.z/4) * 4
BuildBlock:FireServer(X,Y,Z)
end
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if InBuildMode == false then
ShowingPart.Parent = game.ReplicatedStorage
else
ShowingPart.Parent = workspace
local X = math.round(Mouse.Hit.x/4) * 4
local Y = math.round(Mouse.Hit.y/4) * 4
local Z = math.round(Mouse.Hit.z/4) * 4
ShowingPart.Position = Vector3.new(X,Y,Z)
end
end)
ServerScript:
game.ReplicatedStorage.BuildBlock.OnServerEvent:Connect(function(Player,X,Y,Z)
local InstanceBlock = Instance.new("Part")
InstanceBlock.Parent = workspace
InstanceBlock.Size = Vector3.new(4,4,4)
InstanceBlock.Anchored = true
InstanceBlock.Color =
Color3.fromRGB(math.random(0,255),math.random(0,255),math.random(0,255))
InstanceBlock.BrickColor = BrickColor.new("White")
InstanceBlock.Position = Vector3.new(X,Y,Z)
end)
But sometimes I can place a block inside other blocks.
robloxapp-20210621-2118423.wmv (833.5 KB)
Someone tell me how should I do to avoid this happen
.