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

How do i stop a part from leaving a specified area?

Asked by 4 years ago
Edited 4 years ago

I have a tool that creates a part that the player can move around and place into the workspace. The problem i have is that the part doesn't have any limits to where it can go and i can't figure out how. I have already tried the code below but it randomly doesn't detect the "barrier". The only thing it detects is when a player steps on it.

local debounce = true


script.Parent.Touched:Connect(function(hit)
    print("hit")
    if hit.Name == "Barrier" then
        debounce = false
        print(hit.Parent)
        wait(0.5)
        debounce = true
    end
end)

Video of tool: http://www.youtube.com/watch?v=3jmtvgtuU84

0
Use Region3, I'm currently writing a script... maxpax2009 340 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Region 3

Region3 is a form of a datatype and is primarily used to detect if a player is in a certain area. this is used to play music in a certain area, or make a safe zone.

It sounds cumbersome but It's is useful in a lot of games, like my own.

First create a local script, preferably in the starter player.

Create a block that fills your place, another-words create a block where you want the part to stay in. Name it RegionPart

Now enough talking let's get to scripting!

First we need to reference our Region in a variable. And find it's original position so we can object the part to it's general position when it leaves the region.

local Part = game.Workspace.Part -- The parts main objective is to stay in the region
local RegionPart = game.Workspace.RegionPart -- The Part that we created earlier.

local OriginalPosition = Part.Position

local pos1 = RegionPart.Position - (RegionPart.Size / 2) -- Calulating the first position
local pos2 = RegionPart.Position - (RegionPart.Size / 2) -- Calculating The Seconf Position

local Region = Region3.new(pos1, pos2) -- Creating the region itself

while wait() do
    local IsPartInRegion = workspace:FindPartsInRegion3(Region,(-- Insert the parts you want to ignore)

    for i, part in pairs(IsPartInRegion) do -- looping through..
        if part.Parent:FindFirstChild("Part") then
            print("Part is currently in the region")
        else
            print("Part is not in the region!")
            Part.Position = OriginalPosition
        end
    end
end

There you have it!

If you have problems with the script, please reply. I'm home 99% of the time anyway [Joking] All jokes aside; stay safe!

0
The part can still leave the area but it shows "Part is currently in the region" no matter how far i move the part. joefrog123 8 — 4y
0
Oh wait i didn't build the region parts right it works fine thanks joefrog123 8 — 4y
0
no problem man :) maxpax2009 340 — 4y
Ad

Answer this question