Answered by
5 years ago Edited 5 years ago
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
1 | game.Players.PlayerAdded:Connect( function (plr) |
2 | Instance.new( 'Folder' , workspace.Blocks).Name = plr.Name |
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.
01 | local mouse = game.Players.LocalPlayer:GetMouse() |
02 | local OriginalMousePosition = mouse.Hit.p |
04 | local tool = script.Parent |
06 | tool.Equipped:Connect( function () |
10 | tool.Unequipped:Connect( function () |
11 | game.ReplicatedStorage.RemovePassive:FireServer() |
15 | mouse.Button 1 Down:Connect( function () |
17 | game.ReplicatedStorage.PlaceBlock:FireServer(mouse.Hit.p, false ) |
23 | if OrginalMousePosition ~ = mouse.Hit.p then |
24 | OrginalMousePosition = mouse.Hit.p |
25 | game.ReplicatedStorage.RemovePassive:FireServer() |
26 | game.ReplicatedStorage.PlaceBlock:FireServer(mouse.Hit.p, true ) |
Here's what the server script in ServerScriptService would look like
01 | local function IsInt(number) do |
02 | if '.' in tostring (number) then return false else return true |
05 | local function NearestMultiple(number, multiple) |
06 | number = math.floor(number) |
09 | while not IsInt(testNum/multiple) do |
13 | if count < = multiple/ 2 then |
16 | return newNum + multiple |
19 | game.ReplicatedStorage.PlaceBlock.OnServerEvent:Connect( function (plr, pos, passive) |
21 | local block = game.ReplicatedStorage.Blocks [ BlockName ] :Clone() |
22 | local BlockSize = block.Size.X |
23 | local x = NearestMultiple(pos.X, BlockSize) |
24 | local y = NearestMultiple(pos.Y, BlockSize) |
25 | local z = NearestMultiple(pos.Z, BlockSize) |
26 | block.Position = Vector 3. new(x + _, y + _, z + _) |
28 | block.Transparency = 0.5 |
29 | block.CanCollide = true |
30 | block.Name = 'Passive' |
32 | block.Parent = workspace.Blocks [ plr.Name ] |
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!