Need help with this code

Sorry for the bad title wasn’t sure what to put for it.

  1. What do you want to achieve? Keep it simple and clear!
    When the player reaches the edge of the screen prevent them from walking further back
  2. What is the issue? Include screenshots / videos if possible!
    I’m guessing because of the way I have my code setup if you were to hold A and D (backward and forward buttons) at the same time it like bypasses the distance check letting the player walk further back.
  3. What solutions have you tried so far?
    I haven’t really tried much because I’m now sure what to do

Heres the code

--> this code is in a renderstep
		local Vector,IsOnScreen = workspace.CurrentCamera:WorldToScreenPoint(Opponent.HurtBox.Position)
		local Vector = Vector2.new(math.clamp(Vector.X, 0, workspace.CurrentCamera.ViewportSize.X), 0)
		local DistanceFromLeft = (Vector - Vector2.new(0,0)).Magnitude
		local DistanceFromRight = (Vector - Vector2.new(workspace.CurrentCamera.ViewportSize.X,0)).Magnitude
		if math.floor(DistanceFromLeft) <= 190 or math.floor(DistanceFromRight) <= 190 then
			if Modules.CharacterControl.Stuck == false then
				Modules.CharacterControl.Stuck = true
				Modules.CharacterControl:FreezeMovement("WalkSpeed") --> sets walkspeed to 0 so they cant walk further back			
			end 
		else
			if Modules.CharacterControl.Stuck == true then
				Modules.CharacterControl.Stuck = false			
			end 
		end

--> this code runs when they press D and if they're stuck
if Modules.CharacterControl.Stuck then
    Modules.CharacterControl:UnfreezeMovement()
end

Could you add some comments for what these variables/calculations are supposed to do? Can you also provide your module script?

You might be able to fix this issue with no code at all. Just make a invisible wall behind the player to keep it from walking any farther.
This might not work but it might be worth a try.
Sorry if it doesn’t work.

1 Like

I thought about this, but wasn’t sure how I’d get the position for the part although now I guess I could just move it a couple studs back from the humanoidrootpart

The calculations calculate (from my understanding cuz someone helped me out with the calculations) where the opponents hitbox position is on screen and then it calculates the I guess cameras view and checks the distance letting me check if the opponent is near the edge of the screen the point of doing this is honestly I don’t know I just took the idea from street fighter haha

Also which module would you like to see?

One thing you could do is place a part at the middle of the arena and use :GetObjectSpace() to find how far away the player is from said part. If the player is to far math.clamp() the position of the HumanoidRootPart to a max limit.

Well if you were to just add a invisible part behind them you wouldn’t need to code.
Here is an example:

robloxapp-20210731-1250442.wmv (1.4 MB)