Trying to get a NOT exact position

This script:

local RS = game:GetService("ReplicatedStorage")
local Event = RS.KeyPressed

local db = false

Event.OnServerEvent:Connect(function(player, Key)
	local Humanoid = player.Character.Humanoid
	
if db == false then
	db = true	
	if Humanoid.Health > 0 then
		
		local HRP = player.Character.HumanoidRootPart
		
	if Key == "Right" then
			if HRP.Position.X == -8 then
				print("BRUH")	
			else
			HRP.CFrame = HRP.CFrame + Vector3.new(-10,0,0)
           end
	elseif Key == "Left" then
			
			HRP.CFrame = HRP.CFrame + Vector3.new(10,0,0)
			
	elseif Key == "Down" then
				
		  end
	   end
	end	
	wait(.2)
	db = false
end)

Moves the player 10 studs left and right depending on the key they press, and I want to set a limit on how far they can go, so I made an if statement saying this if they are on 8 on the X axis they cannot move that certain way, but although the player will always be on the 8 on the X axis at one point, it isnt going to be exact, maybe it is 8.1092 or 8.93, but never just 8, so how would I check for anything on the 8 on the X axis but not super exact.

1 Like

Is the player moving on a grid based system, or are they able to move around as well which would change their CFrame value.

If it’s grid based then use math.round(whatever value you are checking) in your if statment to round it to the closest integer.

Okay, Ill try it out.

character limit

I did this

if math.round(HRP.Position.X) == 8 then
				print("BRUH")	
			else
			HRP.CFrame = HRP.CFrame + Vector3.new(-10,0,0)
           end

It does not work, am I using it right?

Just printing BRUH only tells you if the condition is met. It doesn’t tell you why.
Try troubleshooting this way:

print(HRP.Position.X) --to see what the actual value is. 
if math.round(HRP.Position.X) < 8 then -- only allows the CFrame to work if the X position is less than the limit of 8
    HRP.CFrame = HRP.CFrame + Vector3.new(-10,0,0)
end

[/quote]

Now, are you trying to count 8 10 stud movements, or you just want the player to only get to a certain X Position in the World Space?

That worked, and I was just trying to get the player in a certain X position. Maybe next time my go to bug fixer wont be printing BRUH.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.