Preventing a player from falling off of the map by using a moving invisible wall

I have a part that stays 6 studs in front of the character at all times unless it’s floating above nothing. It’s supposed to be used to prevent players from walking off the edge of the map. I made it so the part doesn’t move if there’s nothing under the character’s position 6 studs forward, but that means that they can turn while preventing the part from moving and allowing them to walk off the edge:

How would I prevent this from happening?

local SPart = Instance.new("Part");
local Player = game:GetService('Players').LocalPlayer;
repeat game:GetService('RunService').RenderStepped:Wait() until Player.Character;
local Character = Player.Character;
SPart.Anchored, SPart.CanCollide = true, true;
SPart.Parent = workspace;
SPart.Size = Character:GetExtentsSize()
game:GetService('RunService').Stepped:Connect(function()
    local Ray = Ray.new(Character.PrimaryPart.Position + Character.PrimaryPart.CFrame.LookVector * 6, Vector3.new(0,-1,0) * 4);
    local FPOR = workspace:FindPartOnRayWithIgnoreList(Ray, {Character});
    if (FPOR) then
        SPart.CFrame = Character.PrimaryPart.CFrame + Character.PrimaryPart.CFrame.LookVector * 6;
    end
end)

By the by, I poll in this script because for some reason Player.Character would be nil but .CharacterAdded would infinitely yield. :man_shrugging:

EDIT:

For clarification, in my game you’re not supposed to be able to fall and jump when you wish. Such actions are only permitted in certain contexts, and having many walls everywhere would mean that for every context I’d have to remove the nearest walls to the context target, which could easily lead to unexpected behavior. It would also become a hassle for building.

1 Like

Can’t you just put an invisible wall of Anchored = true parts on the server around the edges, so the players cannot walk off?

2 Likes

I’ll have to agree, what’s the point of this unnecessary programming?

2 Likes

Can’t you just use something like this?

You can use Humanoid.MoveDirection (worldspace) to get the unit direction of where the RootPart is moving instead of just checking in front of it.

To get the position x studs in front of where the RootPart is moving, you would do RootPart.Position + Humanoid.MoveDirection * Distance.

No. I don’t want to place invisible walls everywhere on my map, if it were that simple I would’ve done it by now. I should’ve said this earlier, but my game doesn’t permit you to jump normally, but it’s instead a context action. I shouldn’t have to include a snippet of code that removes the boundaries of a wall if they’re going to jump to a specific point, and there’s going to be a multitude of jump contexts within the game.

Nope, I use a current system like that already where if the player is below the lowest part in the map, they’d teleport back to the nearest position that they were on. I just want something better where it prevents them from falling altogether.

2 Likes

It would be easier to add invisible walls at the edges of the map and leave it Anchored.

1 Like

I’m not looking for easy. I’m looking for quality. Having a large amount of individual walls would be very unintuitive for what my game does for movement. Please read my latest response. :stuck_out_tongue:

But this is an unnecessary script, at least it would be easier to make the walls invisible and leave them anchored. I recommend making a folder inside your game, where you know where the invisible walls are, it is easier to look for and also to modify them.

Again, please read my previous responses AND my edit.

This is my actual (test) level geometry:

Placing invisible walls there would not only be tedious, but for a level that has even more complex level geometry, as I said earlier, I’d need to remove walls and replace them for every time they’d need to jump or fall, which would definitely lead to thousands of extra lines total per level. What’s the point of that when I could have a few lines that could do it all for me?

Please, PLEASE do not provide the obvious “simple solution.” This may come off harsh, but I’m not dumb. I would’ve done that so, so long ago if it were fit for my game, however it’s not in this case. There’s no “simple” way to do what I’m doing, I just want it completed.

1 Like

But would it be that tedious? Couldn’t you loop through each part, get its cframe, instantiate a new parts, take that cframe offset them to the left and right, scale their heights, then set their anchor/transparency. It sounds like a task but since you only have to write it once and it’ll loop through all the parts it shouldn’t be too difficult.
I think your solution is really inventive, but I feel like transparent walls would be more efficient 1. to code and 2. for the game’s lag.

You could at least create invisible barriers and at least use the “Scale” tool in those parts, it is not so boring, in less than 1 hour you can do all of that.

Why not put the part closer to your body (say 1-2 studs, and it’ll be harder to fall. Try that. I know you will still have the issue of turning and the block gets stuck well why not make the stud stop a little further off the map and then it wont get stuck. Hope this helps!

You didn’t have a look at my contribution.

1 Like