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

3d building system fails to function as intended, is there a reason why?

Asked by
Joshument 110
5 years ago

Here is my LOCAL script:

local Players = game:GetService("Players")
local Camera = workspace.CurrentCamera
local UIS = game:GetService("UserInputService")
local rs = game:GetService("RunService").RenderStepped
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Ghost = ReplicatedStorage.ItemIndicator
local buildmode = true
local Item = "Plastic Test Brick"
Ghost.Parent = workspace

local mousePos
local mouseTarget
local gridPos
local canPlace

function GetMousePoint(X, Y)
    print("a")
    local RayMag1 = Camera:ScreenPointToRay(X, Y) --Hence the var name, the magnitude of this is 1.
    local NewRay = Ray.new(RayMag1.Origin, RayMag1.Direction * 1000)
    local Target, Position = workspace:FindPartOnRayWithWhitelist(NewRay, workspace.Parts:GetChildren())
    return Position
end

function snap(num) return math.floor(num/4+0.5)*4 end

rs:Connect(function()
    print("b")
    if buildmode then
        print("c")
        Ghost.Parent = workspace
        mousePos = GetMousePoint(UIS:GetMouseLocation().X, UIS:GetMouseLocation().Y)
        gridPos = Vector3.new(snap(mousePos.X), snap(mousePos.Y), snap(mousePos.Z))
        Ghost.CFrame = CFrame.new(gridPos)
        print("d")

        local region = Region3.new(Ghost.Position+Ghost.Size/2.1, Ghost.Position-Ghost.Size/2.1)
        print("l")
        local parts = workspace:FindPartsInRegion3(region, Ghost)

        if #parts > 0 then
            print("e")
            Ghost.Color = Color3.fromRGB(255, 89, 89)
            canPlace = false
        else
            print("f")
            Ghost.Color = Color3.fromRGB(123, 255, 93)
            canPlace = true
        end
    else
        print("g")
        Ghost.Parent = nil
    end
end)

UIS.InputBegan:Connect(function(input)
    print("h")
    if input.UserInputType == Enum.InputType.MouseButton1 then
        if canPlace then
            buildmode = false
            ReplicatedStorage.PlacePart:FireServer(Ghost.Position, Item)
            buildmode = true
            print("i")
        end
        print("j")
    end
    print("k")
end)

Here is my SERVER script, but I don't think the problem is it:

local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")

rs.PlacePart.OnServerEvent:Connect(function(player, position, partType)
    local part = ss:WaitForChild(partType, 5):Clone()
    if part then
        part.Position = position
        part.Parent = workspace
    else
        error("Part Failed to Load")
    end
end)

P.S. There are no errors in the output.

The problem I have is that when using the grid movement, these three things happen:

  • Parts fail to go to the right area
  • Ghost part fails to turn red when region3 detects a problem
  • Bricks do not get placed

Here's a gif if it makes it easier: https://gyazo.com/cdb07c9616926c4bdd99ce4a5eca1d51

I've tried countless times to make this work, but I can't seem to get it right, thank you if you can solve it!

Answer this question