-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
local newBlock = block:Clone()
newBlock:SetPrimaryPartCFrame(pos)
newBlock.Name = block.Name
for i,v in pairs(newBlock:GetChildren()) do
if newBlock.PrimaryPart ~= v then
v.CanCollide = true
else
v.CanCollide = false
end
end
newBlock.Parent = PlacedBlocks
local Placer = Instance.new("ObjectValue", newBlock)
Placer.Name = "Placer"
Placer.Value = player
end)
RemoveBlock.OnServerEvent:Connect(function(player, block)
if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
if block.Parent ~= PlacedBlocks then
if block.Parent:FindFirstChild("Placer") then
if block.Parent.Placer.Value == player then
block.Parent:Destroy()
end
end
end
end
end)
ClearAllBlock.OnServerEvent:Connect(function(player)
for i,v in pairs(PlacedBlocks:GetChildren()) do
if v:FindFirstChild("Placer") and v.Placer.Value == player then
v:Destroy()
end
end
end)
Client:
-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local workspace = game.Workspace
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Models = repStorage:WaitForChild("Models")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = workspace.Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = workspace.Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlocks = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
local SelectionBox = repStorage:WaitForChild("RemoveBox")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.TargetFilter = IgnoredBlocks
local target = nil
local pos = nil
local norm = nil
local isEnabled = false
local canPlace = false
local selectedBlock
local modeldata = {}
local selectedpos = 1
local mode = "build"
local MAX_PLACEMENT_DISTANCE = 360
local RemoveBox = RemoveBlock:Clone()
local fakeBlock
-- // MAIN \\ --
function doFakeMouseStuff()
local ignoreList = {IgnoredBlocks}
local mouseRay = Mouse.UnitRay
local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.unit * MAX_PLACEMENT_DISTANCE)
target, pos, norm = workspace:FindPartOnRayWithIgnoreList(newRay, ignoreList)
end
game:GetService("RunService").RenderStepped:connect(function()
doFakeMouseStuff()
end)
function getblockPos(vec, block)
local posx = math.floor(vec.X / block.PrimaryPart.Size.X + .5) * block.PrimaryPart.Size.X
local posy = math.floor(vec.Y / block.PrimaryPart.Size.Y + .5) * block.PrimaryPart.Size.Y
local posz = math.floor(vec.Z / block.PrimaryPart.Size.Z + .5) * block.PrimaryPart.Size.Z
return Vector3.new(posx, posy, posz)
end
Mouse.Button1Down:connect(function()
if isEnabled == true then
if mode == "build" then -- if the mode is build mode
PlaceBlock:FireServer(selectedBlock, fakeBlock.PrimaryPart.CFrame) -- fires the event to place a block
elseif mode == "remove" then -- if the mode is remove mode
if Mouse.Target ~= nil then -- detects if the Target is nil
if Mouse.Target.Parent:IsA("Model") then -- detects if the Target instance is a Model
RemoveBlock:FireServer(Mouse.Target) -- fires the event and remove the block
end
end
end
end
end)
UIS.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputType ~= Enum.UserInputType.TextInput then
if input.KeyCode == Enum.KeyCode.X then
if isEnabled == true then
if mode == "build" then
mode = "remove"
else
mode = "build"
end
end
end
if input.KeyCode == Enum.KeyCode.E then
if isEnabled == true then
isEnabled = false
else
isEnabled = true
end
end
if input.KeyCode == Enum.KeyCode.R then
local rotation = 0
if fakeBlock.Parent ~= nil then -- detects if the hoverblock is nil
fakeBlock:SetPrimaryPartCFrame(fakeBlock.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(rotation + 45), 0)) -- rotates the block
end
end
if input.KeyCode == Enum.KeyCode.RightControl then
ClearAllBlocks:FireServer() -- deletes the player placed blocks
end
if input.KeyCode == Enum.KeyCode.Q then
if isEnabled == true then
selectedBlock = Models:WaitForChild(modeldata[selectedpos].Name)
fakeBlock:Destroy()
fakeBlock = selectedBlock:Clone()
if selectedpos == #modeldata then
selectedpos = 1
else
selectedpos = selectedpos + 1
end
end
end
end
end)
if selectedBlock == nil then
for i, v in pairs(Models:GetChildren()) do
table.insert(modeldata, v)
local number = math.random(1, i)
selectedBlock = Models:WaitForChild(modeldata[number].Name)
fakeBlock = selectedBlock:Clone()
end
end
while wait() do
if isEnabled == true then
if fakeBlock.Parent == nil then
fakeBlock = selectedBlock:Clone()
fakeBlock.Parent = IgnoredBlocks
end
if mode == "build" then
RemoveBox:Destroy()
if fakeBlock.Parent == nil then
fakeBlock = selectedBlock:Clone()
fakeBlock.Parent = IgnoredBlocks
end
if fakeBlock and pos and norm then
for i,v in pairs(fakeBlock:GetChildren()) do
v.CanCollide = false
v.Transparency = .6
if i > 1 then
fakeBlock.PrimaryPart.Transparency = 1
end
if Mouse.Target ~= nil then
v.Color = Color3.fromRGB(9, 137, 207)
canPlace = true
else
v.Color = Color3.fromRGB(170, 0, 0)
canPlace = false
end
end
doFakeMouseStuff()
local endPos = getblockPos(pos + (norm * (fakeBlock.PrimaryPart.Size * .5)), fakeBlock)
fakeBlock:SetPrimaryPartCFrame(CFrame.new(endPos) * CFrame.Angles(0, math.rad(fakeBlock.PrimaryPart.Orientation.Y), 0))
end
elseif mode == "remove" then
fakeBlock:Destroy()
if RemoveBox.Parent == nil then
RemoveBox = SelectionBox:Clone()
RemoveBox.Parent = IgnoredBlocks
end
if Mouse.Target ~= game.Workspace.Base and Mouse.Target ~= nil then
if Mouse.Target.Parent:IsA("Model") and Mouse.Target.Parent.PrimaryPart ~= nil and Mouse.Target.Parent:FindFirstChild("Placer") then
RemoveBox:SetPrimaryPartCFrame(Mouse.Target.Parent.PrimaryPart.CFrame)
else
RemoveBox:Destroy()
end
else
RemoveBox:Destroy()
end
end
else
if selectedBlock ~= nil then
fakeBlock:Destroy()
RemoveBox:Destroy()
end
end
if fakeBlock.Parent ~= nil or RemoveBox.Parent ~= nil then
if Player.Character.Humanoid.Health < 1 then
game:GetService("Debris"):AddItem(fakeBlock and RemoveBox, 0)
isEnabled = false
end
end
end
-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
local newBlock = block:Clone()
newBlock:SetPrimaryPartCFrame(pos)
newBlock.Name = block.Name
for i,v in pairs(newBlock:GetChildren()) do
if newBlock.PrimaryPart ~= v then
v.CanCollide = true
else
v.CanCollide = false
end
end
newBlock.Parent = PlacedBlocks
local Placer = Instance.new("ObjectValue")
Placer.Parent = newBlock
Placer.Name = "Placer"
Placer.Value = player
end)
RemoveBlock.OnServerEvent:Connect(function(player, block)
if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
if block.Parent ~= PlacedBlocks then
if block.Parent:WaitForChild("Placer") then
if block.Parent.Placer.Value == player then
block.Parent:Destroy()
end
end
end
end
end)
ClearAllBlock.OnServerEvent:Connect(function(player)
for i,v in pairs(PlacedBlocks:GetChildren()) do
if v:FindFirstChild("Placer") and v.Placer.Value == player then
v:Destroy()
end
end
end)
-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
local newBlock = block:Clone()
newBlock:SetPrimaryPartCFrame(pos)
newBlock.Name = block.Name
for i,v in pairs(newBlock:GetChildren()) do
if newBlock.PrimaryPart ~= v then
v.CanCollide = true
else
v.CanCollide = false
end
end
newBlock.Parent = PlacedBlocks
local Placer = Instance.new("ObjectValue")
Placer.Parent = newBlock
Placer.Name = "Placer"
Placer.Value = player.Name
end)
RemoveBlock.OnServerEvent:Connect(function(player, block)
if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
if block.Parent ~= PlacedBlocks then
if block.Parent:WaitForChild("Placer") then
if block.Parent.Placer.Value == player then
block.Parent:Destroy()
end
end
end
end
end)
ClearAllBlock.OnServerEvent:Connect(function(player)
for i,v in pairs(PlacedBlocks:GetChildren()) do
if v:FindFirstChild("Placer") and v.Placer.Value == player then
v:Destroy()
end
end
end)
You can’t set a value to a name otherwise it will be nil
-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
local newBlock = block:Clone()
newBlock:SetPrimaryPartCFrame(pos)
newBlock.Name = block.Name
for i,v in pairs(newBlock:GetChildren()) do
if newBlock.PrimaryPart ~= v then
v.CanCollide = true
else
v.CanCollide = false
end
end
newBlock.Parent = PlacedBlocks
local Placer = Instance.new("ObjectValue", newBlock)
Placer.Name = "Placer"
Placer.Value = player
end)
RemoveBlock.OnServerEvent:Connect(function(player, block)
if block.Parent ~= nil or block.Parent ~= workspace or block ~= "Base" then
if block.Parent ~= PlacedBlocks then
if block.Parent:FindFirstChild("Placer") then
if block.Parent.Placer.Value == player then
block.Parent:Destroy()
end
end
end
end
end)
ClearAllBlock.OnServerEvent:Connect(function(player)
for i,v in pairs(PlacedBlocks:GetChildren()) do
if v:FindFirstChild("Placer") and v.Placer.Value == player then
v:Destroy()
end
end
end)
into this:
-- // SERVICES \\ --
local repStorage = game:GetService("ReplicatedStorage")
-- // WORKSPACE MODELS \\ --
local Base = game.Workspace.Base
-- // FOLDERS \\ --
local Events = repStorage:WaitForChild("Events")
local Modules = repStorage:WaitForChild("Modules")
-- // WORKSPACE FOLDERS
local IgnoredBlocks = Base:WaitForChild("IgnoredBlocks")
local PlacedBlocks = Base:WaitForChild("PlacedBlocks")
-- // EVENTS \\ --
local ClearAllBlock = Events:WaitForChild("ClearAllBlocks")
local PlaceBlock = Events:WaitForChild("PlaceBlock")
local RemoveBlock = Events:WaitForChild("RemoveBlock")
-- // VARIABLES \\ --
-- // MAIN \\ --
PlaceBlock.OnServerEvent:Connect(function(player, block, pos)
local newBlock = block:Clone()
newBlock:SetPrimaryPartCFrame(pos)
newBlock.Name = block.Name
for i,v in pairs(newBlock:GetChildren()) do
if newBlock.PrimaryPart ~= v then
v.CanCollide = true
else
v.CanCollide = false
end
end
newBlock.Parent = PlacedBlocks
local Placer = Instance.new("StringValue", newBlock)
Placer.Name = "Placer"
Placer.Value = player.Name
end)
RemoveBlock.OnServerEvent:Connect(function(player, block)
if block ~= nil and block ~= "Workspace" and block ~= "Base" then
if block.Parent ~= PlacedBlocks then
if block.Parent:FindFirstChild("Placer") or block:FindFirstChild("Placer") then
if block.Parent.Placer.Value == player.Name then
block.Parent:Destroy()
end
end
end
end
end)
ClearAllBlock.OnServerEvent:Connect(function(player)
for i,v in pairs(PlacedBlocks:GetChildren()) do
if v:FindFirstChild("Placer") and v.Placer.Value == player.Name then
v:Destroy()
end
end
end)