How to Create Invisible Wall that slows Character down when Close

How should I create an invisible wall that makes you walk slower the closer you are to it?

I doesn’t know what do you talking about, do you want to know how to make invisible walls?

you can reply about details and script to make me know

magnitude checking a vector3 position (instance position) and comparing distance, then having a math.clamp calculation which gradually slows the player
example: player walkspeed is 16, when wall to player distance is less than 50 studs for every stud closer negate 0.1 walkspeed from humanoid, apply math.clamp to limit player from getting too slow

I will try this, not sure if I will be able to code it.

A quick way to do this is like so:

In a LocalScript inside StarterPlayerStarterCharacterScripts

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local wall = workspace.Wall --// or whatever the wall is
local minSpeed, maxSpeed = 0.1, 16
--// setting minDist to 0 will make it so that being inside the wall
--// will make the player have 0 speed
local minDist, maxDist = 3, 15

--// depending on the orientation of the wall, use either one of these
local WALL_X_POS = wall.Position.X
local WALL_Z_POS = wall.Position.Z

--// linear interpolation
function lerp(a, b, t)
	return a + (b - a) * t
end
--// inverse linear interpolation
function inverse_lerp(a, b, x)
	return (x - a) / (b - a)
end

RunService.RenderStepped:Connect(function()
	--// for this example, we are using the x position of the wall
	--// (x axis of the wall is the boundary)
	local dist = math.abs(WALL_X_POS - humanoidRootPart.Position.X)
	local perc = math.clamp(inverse_lerp(minDist, maxDist, dist), 0, 1)
	local speed = lerp(minSpeed, maxSpeed, perc)
	humanoid.WalkSpeed = speed
end)

The result looks like so:

Edit: If your walls have varied orientation, the script is more complicated. This script is meant to be a simple solution for walls placed in the X and Z directions ONLY!

wow, how do you can do that?

sorry but don't click this

min chars

That looks amazing! How would you get that to work using a script parented to the part?

I assume the script is in StarterCharacterScripts, as he references the wall as another variable.

You can’t have the script parented to the part because in order for it to work, it needs to be parented to a descendant of the Player object! In this case, it is parented to the Character, which is a descendant of the Player.

Is it possible to rewrite the code so it can be parented to the part though?

This is not possible either. In order for the script to run as smoothly as it does, it needs to constantly update every frame. This is only possible through RunService.RenderStepped, and this is only possible by using this through a LocalScript that is parented to a descendant of the Player object.

Also, it’s bad practice to parent scripts like this to parts because they have no correlation to eachother. Instead, this should be added to a script that controls the Character.

I am trying to make this work for multiple walls. I have tried this but it does not seem to work. Does anyone know the error I made?

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local minSpeed, maxSpeed = 0.1, 16
-- setting minDist to 0 will make it so that being inside the wall
-- will make the player have 0 speed
local minDist, maxDist = 3, 15


-- linear interpolation
function lerp(a, b, t)
	return a + (b - a) * t
end
-- inverse linear interpolation
function inverse_lerp(a, b, x)
	return (x - a) / (b - a)
end

RunService.RenderStepped:Connect(function()
	-- workspace.InvisibleWalls is a folder with parts in
	local walls = workspace.InvisibleWalls:GetChildren()
	
	for i, wallObj in ipairs(walls) do
		-- depending on the orientation of the wall, use either one of these
		local WALL_X_POS = wallObj.Position.X
		local WALL_Z_POS = wallObj.Position.Z
		
		if wallObj.Orientation.Y == 0 or math.abs(wallObj.Orientation.Y) == 180 then
			local dist = math.abs(WALL_X_POS - humanoidRootPart.Position.X)
			local perc = math.clamp(inverse_lerp(minDist, maxDist, dist), 0, 1)
			local speed = lerp(minSpeed, maxSpeed, perc)
			humanoid.WalkSpeed = speed
			
		elseif math.abs(wallObj.Orientation.Y) == 90 then
			local dist = math.abs(WALL_Z_POS - humanoidRootPart.Position.Z)
			local perc = math.clamp(inverse_lerp(minDist, maxDist, dist), 0, 1)
			local speed = lerp(minSpeed, maxSpeed, perc)
			humanoid.WalkSpeed = speed
			
		end
	end
end)