Prevent players from falling off the map using script

Is there a way to prevent players from falling off the map using script rather than putting invisible walls around the map manually?

2 Likes

Yes.

You could use Region3 and check the HumanoidRootPart’s Position.

1 Like

How does this help? What are you supposed to do when you detect the player out of boundary?

1 Like

Just check

if HumanoidRootPart.Position.Y < 0 then

--TP Back To Spawn

end

Does not have to be Y < 0, it can be any number you want

1 Like

Something along the lines of if they’re within one of the Region3 boundaries you make, CFrame their HumanoidRootPart back into bounds.

All of the pieces are there, you just have to assemble them in order to do your specific thing.

2 Likes

While this would work I do not want to teleport them back. I simply want to make them feel like they are running into a wall and can’t fall down.

1 Like

I’m just giving you an idea of how to do it, do what you want with your Script. :+1:

You can use Region3 and detect when the Player is outside of the Area and then TP that Player back.

1 Like

I was just wondering if there was a simple property you can set on the baseplate to prevent this from happening. I guess there isn’t. If I have to manually update the CFrame probably on every render frame that sounds some what expensive. Not sure if I want to do that. I’ll just put up transparent walls.

1 Like

afaik there is not

but there’s definitely many different ways you could grab the edges of the map scriptwise and create invisible boundaries with bricks that way

i guess it depends on the # of maps and the ease you want to have if/when you add new ones

2 Likes

You mean create invisible parts on the fly and put up walls dynamically instead of manually putting them up in Studio editor right? That sounds like a cool idea.

2 Likes

I mean you could do it like that, but I guess I meant more so you’d create the invisible parts using scripts once around the map (either wherever you store the map, serverside, or clientside).

That way you wouldn’t have to place them by hand and that way it would ideally function for any and all maps you’d create.

edit: food for thought, you probably could use Region3 to create parts on the fly to block the player and/or make the player stop walking/turn around. Maybe use it as a buffer, not the absolute edge?

1 Like

I should have been more clear with my question. It’s not just the entire map. It’s any flat platform I choose to trap them in. I probably won’t do any Region3 buffer idea. While it sounds like a fun coding exercise to do, I just feel invisible walls are cleaner and guaranteed solution. I just have to come up with a way to calculate all the positioning and dimensions of the invisible walls given any flat floor part.

3 Likes

From my five minutes of testing, this is what I came up with;

local Region = Region3.new(Vector3.new(-10,0,-10), Vector3.new(10,50,10))

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Root = character.PrimaryPart
		while wait() do
			if character.HumanoidRootPart.Position.X >= Region.Size.X then
				character:SetPrimaryPartCFrame(CFrame.new(Vector3.new(Region.Size.X,Root.Position.Y,Root.Position.Z)) * 
					CFrame.Angles(math.rad(Root.Orientation.X),math.rad(Root.Orientation.Y),math.rad(Root.Orientation.Z)))
			end
		end
	end)
end)

Keep in mind this is horribly written, but basically it just prevents the player from walking beyond the one edge of the Region. It would be wise to clean it up.

Edit:
I can do a better job than that…

Whilst this is still not great, it can give a working example of some form of invisible walls using a Region3. Obviously you would want to clean it up.

local Region = Region3.new(Vector3.new(-10,0,-10), Vector3.new(10,50,10))

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local Root = character.PrimaryPart
		while wait() do
			if Root.Position.X >= Region.CFrame.p.X + (Region.Size.X / 2) then
				Root.CFrame = CFrame.new(Region.CFrame.p.X + (Region.Size.X / 2), Root.Position.Y, Root.Position.Z) *
					CFrame.Angles(math.rad(Root.Orientation.X), math.rad(Root.Orientation.Y), math.rad(Root.Orientation.Z))
			end
			if Root.Position.X <= Region.CFrame.p.X - (Region.Size.X / 2) then
				Root.CFrame = CFrame.new(Region.CFrame.p.X - (Region.Size.X / 2), Root.Position.Y, Root.Position.Z) *
					CFrame.Angles(math.rad(Root.Orientation.X), math.rad(Root.Orientation.Y), math.rad(Root.Orientation.Z))
			end
			if Root.Position.Z >= Region.CFrame.p.Z + (Region.Size.Z / 2) then
				Root.CFrame = CFrame.new(Root.Position.X, Root.Position.Y, Region.CFrame.p.X + (Region.Size.Z / 2)) *
					CFrame.Angles(math.rad(Root.Orientation.X), math.rad(Root.Orientation.Y), math.rad(Root.Orientation.Z))
			end
			if Root.Position.Z <= Region.CFrame.p.X - (Region.Size.Z / 2) then
				Root.CFrame = CFrame.new(Root.Position.X, Root.Position.Y, Region.CFrame.p.X - (Region.Size.Z / 2)) *
					CFrame.Angles(math.rad(Root.Orientation.X), math.rad(Root.Orientation.Y), math.rad(Root.Orientation.Z))
			end
		end
	end)
end)
1 Like

I wouldn’t want to do a “while wait() do” check. This is expensive. And especially when I could have many platforms in my game where players can’t fall off the edge. Like I said, I should have specified my problem more.

That’s why I said it was a horribly written piece of code, and shouldn’t be used.

I thought by horrible you meant the code formatting style like indentation. Didn’t realize you meant the efficiency of it.

I meant pretty much all of it.

While I appreciate your efforts, I do not want to mislead other beginner developers to think that the approach of doing constant Region checks either in while loops or render frame steps is a good idea. This is very expensive operation and should be used with caution. I want to leave this note along with your code in this thread. I think putting up the wall and let the game engine prevent it for you is better. And be able to build the walls around any given part in your script is even better.

2 Likes