So, basically, I’m making this top down game. Currently a big unsolved issue I’m having is that players can walk off edges, which I don’t want to happen. I’ve tried a couple solutions I’ve seen on here, but they haven’t worked too well with how my game works. I’m not going to put invisible walls on every edge, by the way. That would be far too tedious and annoying, and there is definitely the chance of me forgetting or there being some small spot you can glitch through.
Example of my failed attempt: (i got this “solution” from another post here its not really mine)
Unfortunately, anyway to stop a player from going somewhere is a pain in the behind… Having that said, most popular games DO use invisible walls which i’m aware you don’t want.
The only ways that i’m aware of for you to block a player from entering an area is either,
- Use Invisible Walls,
- Use Area Detectors for Collision Colliding,
- Adding Terrain/Decorations.
try raycasting under the part and check if a part is within a reasonable amount maybe like 5 studs
I think a way you could go about solving this is by casting a ray down onto the floor and getting the information of the part. Then, you could either construct invisible walls all around the part with a bit of maths or use an algorithm to get all connected parts and make one giant wall.
With the first idea, you could use rays to see if the part is connected to any other parts, and if it is you place a cancollide part which detects when the user walks through it, allowing for the process to repeat itself. If there isn’t another part connected to it, place a wall which can’t be walked through.
pretty scuffed code and very hacky but this seems to work for me
(improvements can obviously be made with math and stuff)
local UP = Vector3.new(0, 1, 0)
local Map = workspace
local player = game.Players.LocalPlayer --// or use any way to get the player it all works
local Filter = RaycastParams.new()
Filter.FilterType = Enum.RaycastFilterType.Whitelist
Filter.FilterDescendantsInstances = Map:GetDescendants();
local invisWall = Instance.new("Part")
invisWall.Transparency = 1;
invisWall.CanCollide = true;
invisWall.Anchored = true;
invisWall.Name = "-SCRIPT-InvisWall"
local walls = {};
local function check(d)
local edge = workspace:Raycast(player.Character.HumanoidRootPart.Position + d * 2, player.Character.HumanoidRootPart.CFrame.UpVector * -12, Filter)
if not edge then
for i, v in next, walls do
if (player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector*3 - v.Position).Magnitude < 0.1 then
return;
end
end
local closestWall;
local i = 3;
while not closestWall and i > -1 do
i-=0.1;
closestWall = workspace:Raycast(player.Character.HumanoidRootPart.Position + d*i, player.Character.HumanoidRootPart.CFrame.UpVector * -12, Filter)
end
if closestWall then
local iw = invisWall:Clone()
iw.CFrame = CFrame.new(closestWall.Position)
iw.CFrame = CFrame.new(iw.Position, iw.Position + UP)
iw.Size = Vector3.new(1, 1, 1000)
iw.Parent = workspace
game.Debris:AddItem(iw, 3) -- optimization sorta
local me = #walls+1
walls[me] = iw
task.delay(3, function()
walls[me] = walls[#walls]
walls[#walls] = nil;
end)
end
end
end
game["Run Service"].RenderStepped:Connect(function()
task.spawn(check, player.Character.HumanoidRootPart.CFrame.LookVector)
task.spawn(check, player.Character.HumanoidRootPart.CFrame.RightVector)
task.spawn(check, -player.Character.HumanoidRootPart.CFrame.LookVector)
task.spawn(check, -player.Character.HumanoidRootPart.CFrame.RightVector)
end)
it raycasts every direction and checks if there is an edge if so places and invisible wall