Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to place a block with the mouse using mouse.Hit.Position?

Asked by
sheepposu 561 Moderation Voter
5 years ago

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

01local uis = game:GetService('UserInputService')
02local plr = game.Players.LocalPlayer
03local mouse = plr:GetMouse()
04 
05if uis.MouseEnabled then
06    local OriginalMousePos = mouse.Hit.Position
07    mouse.Button1Down:Connect(function()
08        if plr.Mode.Value == 'Building' and plr.Block.Value ~= '' then --Mode is the mode they are in (None, Building, or Removing) and Block is the block they are using (string of the block's name)
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 --Size of the block
14            local base = workspace.Plots[plr.Plot.Value].Bases.Base --base they are building on
15            local downX = base.Position.X - (base.Size.X / 2)
View all 47 lines...

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

01local PS = game:GetService('PhysicsService')
02 
03game.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 = Vector3.new(bs, bs, bs)
07    if passive then
08        block.CanCollide = false
09        block.Transparency = 0.5
10        block.Name = 'Passive'
11        PS:SetPartCollisionGroup(block, 'Building')
12    else
13        PS:SetPartCollisionGroup(block, 'Built')
14        plr.Blocks[block.Name].Value = plr.Blocks[block.Name].Value - 1
15        plr.Refresh.Value = true
16    end
17    block.Parent = workspace.Plots[plr.Plot.Value].Blocks
18end)

Removing a Passive in ServerScriptService

1game.ReplicatedStorage["Remove Passive"].OnServerEvent:Connect(function(plr)
2    pcall(function()
3        workspace.Plots[plr.Plot.Value].Blocks.Passive:Destroy()
4    end)
5end)

Thx in advance

0
You should do your calculations on the server to prevent hackers OBenjOne 190 — 5y
0
Alright, thx sheepposu 561 — 5y

1 answer

Log in to vote
0
Answered by
SmartNode 383 Moderation Voter
5 years ago

Best bet is to not make it so difficult with your code. Obviously this is probably your first take with some fixed errors. Since instance positioning has an anchor right in the middle of the instance, you can split the instance into half, and then if there is a block below it or above it, simply half that too, then add those two halfed values and then position the new block wherever the sum position is at. No need to do math.floor and hacks for this.

0
Alright, I'll take everything you're saying into consideration sheepposu 561 — 5y
Ad

Answer this question