✓ Accepted answer
I actually achieved the result you are looking for recently so I can help you. First off, the blocks can go in Replicated storage. Next, for a Minecraft type game, you should make a folder in the workspace called "Blocks" and have a server script in ServerScriptService do the following
game.Players.PlayerAdded:Connect(function(plr)
Instance.new('Folder', workspace.Blocks).Name = plr.Name
end)
Then you can move all the blocks a player places into their folder which may come in handy later.
You should get the mouse coords and everything and send it to a server script in ServerScriptService. If the block is placed from a LocalScript only the player who placed it will see the block. Also, if a localscript does all the math, it can be exploited by hackers. Here's a example of what your LocalScript might look like. I put a RemoteEvent in ReplicatedStorage called PlaceBlock. I also made another event called RemovePassive that will be used to remove passive blocks.
local mouse = game.Players.LocalPlayer:GetMouse()
local OriginalMousePosition = mouse.Hit.p
local Active = false
local tool = script.Parent
tool.Equipped:Connect(function()
Active = true
end)
tool.Unequipped:Connect(function()
game.ReplicatedStorage.RemovePassive:FireServer()
Active = false
end)
mouse.Button1Down:Connect(function()
if Active then
game.ReplicatedStorage.PlaceBlock:FireServer(mouse.Hit.p, false)
end
end)
while wait() do
if Active then
if OrginalMousePosition ~= mouse.Hit.p then
OrginalMousePosition = mouse.Hit.p
game.ReplicatedStorage.RemovePassive:FireServer()
game.ReplicatedStorage.PlaceBlock:FireServer(mouse.Hit.p, true)
end
end
end
Here's what the server script in ServerScriptService would look like
local function IsInt(number) do
if '.' in tostring(number) then return false else return true
end
local function NearestMultiple(number, multiple)
number = math.floor(number)
local newNum = number
local count = 0
while not IsInt(testNum/multiple) do
newNum = testNum - 1
count = count + 1
end
if count <= multiple/2 then
return newNum
else
return newNum + multiple
end
game.ReplicatedStorage.PlaceBlock.OnServerEvent:Connect(function(plr, pos, passive)
BlockName =
local block = game.ReplicatedStorage.Blocks[BlockName]:Clone()
local BlockSize = block.Size.X
local x = NearestMultiple(pos.X, BlockSize)
local y = NearestMultiple(pos.Y, BlockSize)
local z = NearestMultiple(pos.Z, BlockSize)
block.Position = Vector3.new(x + _, y + _, z + _)
if passive then
block.Transparency = 0.5
block.CanCollide = true
block.Name = 'Passive'
end
block.Parent = workspace.Blocks[plr.Name]
end)
It should look something like this
You can do the RemovePassive script, but let me tell you to use a "pcall(function()" in it, for when it is fired without there being a passive block.
Hope this helps!
6 comments
Does not work, see my below answer for more
I got it to function
Check my answer's comments for more info, on things i cant get to wrok
mostly solved my issue, can u please help me on getting the building transparent thing that follow mouse, so its in a cube like form
Any errors. It's the same as placing a black, but it's name is Passive, it is 50% transparent, and can't collide
Any errors. It's the same as placing a block, but it's name is Passive, it is 50% transparent, and can't collide