How to place a block with the mouse using mouse.Hit.Position?
I'm not having errors and it works, but it's really finicky. I have a LocalScript to do all the math and everything and then I send it through an event to place it with a ServerScript. I was hoping someone could improve my code to make it better.
Here is the LocalScript inside StarterGui
01 | local uis = game:GetService( 'UserInputService' ) |
02 | local plr = game.Players.LocalPlayer |
03 | local mouse = plr:GetMouse() |
05 | if uis.MouseEnabled then |
06 | local OriginalMousePos = mouse.Hit.Position |
07 | mouse.Button 1 Down:Connect( function () |
08 | if plr.Mode.Value = = 'Building' and plr.Block.Value ~ = '' then |
09 | local pos = mouse.Hit.Position |
10 | local x = math.floor(pos.X) |
11 | local y = math.floor(pos.Y) |
12 | local z = math.floor(pos.Z) |
13 | local bs = plr.BlockSize.Value |
14 | local base = workspace.Plots [ plr.Plot.Value ] .Bases.Base |
15 | local downX = base.Position.X - (base.Size.X / 2 ) |
16 | local upX = base.Position.X + (base.Size.X / 2 ) |
17 | local downZ = base.Position.Z - (base.Size.Z / 2 ) |
18 | local upZ = base.Position.Z + (base.Size.Z / 2 ) |
19 | local coords = Vector 3. new(x + bs/ 2 , y + bs/ 2 , z + bs/ 2 ) |
20 | if coords.X > downX and coords.X < upX and coords.Z > downZ and coords.Z < upZ then |
21 | game.ReplicatedStorage [ "Remove Passive" ] :FireServer() |
22 | game.ReplicatedStorage [ "Place Block" ] :FireServer(plr.Block.Value, coords, bs, false ) |
28 | if OriginalMousePos ~ = mouse.Hit.Position and plr.Mode.Value = = 'Building' and plr.Block.Value ~ = '' then |
29 | local pos = mouse.Hit.Position |
30 | OriginalMousePos = pos |
31 | local x = math.floor(pos.X) |
32 | local y = math.floor(pos.Y) |
33 | local z = math.floor(pos.Z) |
34 | local bs = plr.BlockSize.Value |
35 | local base = workspace.Plots [ plr.Plot.Value ] .Bases.Base |
36 | local downX = base.Position.X - (base.Size.X / 2 ) |
37 | local upX = base.Position.X + (base.Size.X / 2 ) |
38 | local downZ = base.Position.Z - (base.Size.Z / 2 ) |
39 | local upZ = base.Position.Z + (base.Size.Z / 2 ) |
40 | local coords = Vector 3. new(x + bs/ 2 , y + bs/ 2 , z + bs/ 2 ) |
41 | if coords.X > downX and coords.X < upX and coords.Z > downZ and coords.Z < upZ then |
42 | game.ReplicatedStorage [ "Remove Passive" ] :FireServer() |
43 | game.ReplicatedStorage [ "Place Block" ] :FireServer(plr.Block.Value, Vector 3. new(x + bs/ 2 , y + bs/ 2 , z + bs/ 2 ), bs, true ) |
If you are wondering why I added half the size of the block when firing the event to place the block, it's to center the block on the plot.
If you need it, here are the scripts for placing a block and removing a passive.
Placing a Block in ServerScriptService
01 | local PS = game:GetService( 'PhysicsService' ) |
03 | game.ReplicatedStorage [ "Place Block" ] .OnServerEvent:Connect( function (plr, block, coords, bs, passive) |
04 | local block = game.ReplicatedStorage.Blocks [ block ] :Clone() |
05 | block.Position = coords |
06 | block.Size = Vector 3. new(bs, bs, bs) |
08 | block.CanCollide = false |
09 | block.Transparency = 0.5 |
10 | block.Name = 'Passive' |
11 | PS:SetPartCollisionGroup(block, 'Building' ) |
13 | PS:SetPartCollisionGroup(block, 'Built' ) |
14 | plr.Blocks [ block.Name ] .Value = plr.Blocks [ block.Name ] .Value - 1 |
15 | plr.Refresh.Value = true |
17 | block.Parent = workspace.Plots [ plr.Plot.Value ] .Blocks |
Removing a Passive in ServerScriptService
1 | game.ReplicatedStorage [ "Remove Passive" ] .OnServerEvent:Connect( function (plr) |
3 | workspace.Plots [ plr.Plot.Value ] .Blocks.Passive:Destroy() |
Thx in advance