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

Why is my Region3 being inconsistent?

Asked by 5 years ago

I am working on a building system for my game, and I check if you are building on your plot by creating a temporary part where you are attempting to build, and if it is in the Region3 of that plot, then it places the object. My problem is that after some blocks have been placed, some regions of the Region3 go unchecked, returning that the temporary part is not in the region.

Video Example

Code:

game.ReplicatedStorage.Buildables.reqBuild.OnServerEvent:Connect(function(player, pos, item, color, material, color2, material2, hazard)

    local function build()
        local piece = game.ReplicatedStorage.Buildables:WaitForChild(item)
        local part = piece:Clone()
        part:SetPrimaryPartCFrame(pos)
        for _, object in pairs(part:GetChildren()) do
            -- Decorates object
        end
        part.Parent = game.Workspace.Placed_Blocks
        game.ReplicatedStorage.Buildables.reqBuild:FireClient(player)
    end

    local tempPart = Instance.new("Part")
    tempPart.Size = Vector3.new(1, 1, 1)
    tempPart.CanCollide = false
    tempPart.Transparency = 1
    tempPart.Position = pos.p -- Requested position from CFrame to Vector3
    tempPart.Parent = workspace.Ignore_Blocks
    tempPart.Name = "tempPart_" .. player.Name
    tempPart.Anchored = true
    workspace.Ignore_Blocks:WaitForChild("tempPart_" .. player.Name)
    for _, object in pairs(workspace.Stuff.Build_Plates:GetChildren()) do
        if object.Owner.Value == player.Name then
            --The buildplate has 2 Vector3 values for the corners of the area.
            --Corner1 contains the lowest values, Corner2 contains the higher
            local region = Region3.new(object.Corner1.Value, object.Corner2.Value)
            for _, object in pairs(workspace:FindPartsInRegion3(region)) do
                print(object.Name)
                if object == tempPart then
                    tempPart:Destroy()
                    tempPart = nil
                    build()
                end
            end
            region = nil
            if tempPart then
                tempPart:Destroy()
                tempPart = nil
                game.ReplicatedStorage.Buildables.reqBuild:FireClient(player)
            end
        end
    end

end)
0
Have you tried raising corner2? firestarroblox123 440 — 5y
0
Yes, it's broken at every Y-value for corner2 rebot333 40 — 5y
0
Have you tried lowering corner1? firestarroblox123 440 — 5y
0
Also why do you have that section after line 35? firestarroblox123 440 — 5y
0
region = nil, tempPart:Destory(), and tempPart = nil is unnecessary, sorry, but that just tells the client that it acknowledged the object. rebot333 40 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I was able to get it to work by without the region or the temp part. This is also a lot more efficient.

New code:

game.ReplicatedStorage.Buildables.reqBuild.OnServerEvent:Connect(function(player, pos, item, color, material, color2, material2, hazard)

    local function build()
        local piece = game.ReplicatedStorage.Buildables:WaitForChild(item)
        local part = piece:Clone()
        part:SetPrimaryPartCFrame(pos)
        for _, object in pairs(part:GetChildren()) do
            -- Decorates object
        end
        part.Parent = game.Workspace.Placed_Blocks
        game.ReplicatedStorage.Buildables.reqBuild:FireClient(player)
    end

    local p3 = pos.p
    local accept = false
    for _, object in pairs(workspace.Stuff.Build_Plates:GetChildren()) do
        if object.Owner.Value == player.Name then
            local c1 = object.Corner1.Value
            local c2 = object.Corner2.Value
            if p3.x >= c1.x and p3.x <= c2.x and p3.y >= c1.y and p3.y <= c2.y and p3.z >= c1.z and p3.z <= c2.z then
                accept = true
                build()
            end
        end
    end
    if accept == false then
        game.ReplicatedStorage.Buildables.reqBuild:FireClient(player)
    end

end)

Sorry to anyone in the future who was having the same problem and absolutely needs the Region3.

Ad

Answer this question