Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

Use Region3, I'm currently writing a script...

1 answer

✓ Accepted answer

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!

3 comments

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.

0

Oh wait i didn't build the region parts right it works fine thanks

0

no problem man :)

This discussion is preserved as it was. New answers, comments, and votes are closed.