How can I check if the player is touching the edge of the screen? I’m trying to make a system that sets their walk speed to 0 to prevent them from moving back my current code works although it’s not really accurate and breaks occasionally.
--I use to check the distance from left and right but realized I could check one
local function DistanceFromLeft(HRPPos)
local Vector,IsOnScreen = workspace.CurrentCamera:WorldToScreenPoint(HRPPos)
local Vector = Vector2.new(math.clamp(Vector.X, 0, workspace.CurrentCamera.ViewportSize.X), 0)
return (Vector - Vector2.new(0,0)).Magnitude
end
local Max1 = 200
local Max2 = 1200
warn(math.floor(DistanceFromLeft(HRP.Position)))
if (DistanceFromLeft(HRP.Position) <= Max1 and DistanceFromLeft(Player2.HumanoidRootPart.Position) >= Max2) or (DistanceFromLeft(HRP.Position) >= Max2 and DistanceFromLeft(Player2.HumanoidRootPart.Position) <= Max1) then
--| if they're at the edge of the screen, but walking forward then give them speed back
if (UIS:IsKeyDown(Enum.KeyCode.D) and Character.CurrentFacingDirection == 1) or
UIS:IsKeyDown(Enum.KeyCode.A) and (Character.CurrentFacingDirection == -1) then
warn("Moving away from edge")
Character:AdjustMovement("WalkSpeed",17)
else
--| they're at the edge of the screen lower walkspeed so they can't walk further back
warn("Both players at edge of screen")
Character:AdjustMovement("WalkSpeed",0)
end
end