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

Broken building script, block going inside parts in certain directions. How do I fix this?

Asked by 6 years ago

So I've decided to try and make a minecraft-like building tool, and I've gotten pretty far. It snaps to a 4x4 grid, and for the most part, it works. But only in two directions. In two directions (South and East) it works just fine, and moves to the side of the part I'm building on. But in the other two directions (North and West) it moves inside the part I'm building on. Similarly, it works just fine if my mouse is on the top of a part, but it moves inside of the part if my mouse is on the bottom of it. I'm not sure exactly why this is happening and I'd really like it if someone could help me. Here's the script. Don't mind the PlaceBlock event firing, that just places the part where the fakepart is.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local fakepart

function roundAToB(a,b)
    return(math.floor((a/b)+0.5)*b)
end

script.Parent.Equipped:Connect(function()
    fakepart = game.ReplicatedStorage.PlacedPart:Clone()
    fakepart.Parent = game.Workspace
    fakepart.Transparency = 0.6
    fakepart.CanCollide = false
    local noselecttag = Instance.new("BoolValue", fakepart)
    noselecttag.Name = "NoSelect"
    mouse.TargetFilter = fakepart
end)

script.Parent.Unequipped:Connect(function()
    if fakepart then
        fakepart:Destroy()
    end
end)

script.Parent.Activated:Connect(function()
    if fakepart then
        script.Parent.PlaceBlock:FireServer(fakepart.CFrame)
    end
end)

game:GetService("RunService").RenderStepped:Connect(function()
    if fakepart then
        local hit = mouse.Hit
        fakepart.Position = Vector3.new(math.floor(roundAToB(hit.p.x, 4)), math.floor(roundAToB(hit.p.y, 4)), math.floor(roundAToB(hit.p.z, 4)))
    end
end)

Answer this question