Tracking if Player is Within Coordinates

I want this script to detect if the player is within a certain offset of a part. In my game there are plots, and the corner of each plot is defined by a certain offset. If the player is within the plot it should print the player is within the plot.

The issue is that it states the player is within the plot when the player is most definitely not.

So far I’ve retraced the steps and did the math on calculator and the math even says the code is wrong. Idk if it’s a minor mistake I made or a bug of some sort.

Here is the local script that is within the PlayerStartScripts:

local player = game.Players.LocalPlayer
local PlotCenter = player.HomePosition

local minX = PlotCenter.Value.X - 24
local maxX = PlotCenter.Value.X + 24
local minZ = PlotCenter.Value.Z - 19
local maxZ = PlotCenter.Value.Z + 19

player.CharacterAdded:Connect(function(character)
	local RootPart = character.PrimaryPart
	while wait(2) do
		local position = RootPart.Position
		local x = position.X
		local z = position.Z

		if x >= minX and x <= maxX and z >= minZ and z <= maxZ then
			print("Player is within the plot area.")
			-- Add your logic here (e.g., allow building, perform actions, etc.)
		else
			print("Player is outside the plot area.")
			-- Add any other logic you need (e.g., restrict actions, display messages, etc.)
		end
	end
end)
1 Like

What happens if you try to check the distance between the player and the center instead of check the position?

It does actually get the distance from the plot center.

What I meant is a magnitude check, for example

local Distance = math.abs(PlotCenter.Value.Magnitude - position.Magnitude)
if Distance < 40 then
        -- player is in plot
end

The problem with that is that makes the boundary around the plot a circle, I need the boundary to be a precise square.

What about using methods such as GetPartsInPart to check if the player is in the area?

Did you mean :GetTouchingParts() because I couldn’t find a function called GetPartsInPart

I meant this: GetPartsInPart
I think its workspace:GetPartsInPart()

I would like whatever function to be relative to the player and ran through a local script and I don’t think Workspace:GetPartsInPart() is possible to make relative to the player.

I don’t see anything wrong with the logic in your code snippet, and I’m genuinely confused why it wouldn’t be working. Did you confirm that minX, maxX, minZ, maxZ are all correct?

Also, what did you mean that the math even says the code is wrong?

1 Like

Workspace:GetPartsInPart() returns a table of all parts inside a certain part, so what you could do is check if for example the HumanoidRootPart of the player is inside the table, however I am not sure if this is the ideal solution.

1 Like

When I did the math by getting the players X position and offset from the center, the player was inside of the min and max of both X and Z, though the script says that the player is outside of them, vice versa.

Yea, because then I would need to cycle through a list of plots and find the one the player owns, then I would need to check if the player is inside of it’s boundaries. I feel like there is less resource intensive ways to check that.

1 Like

The min and max of the X’s and Z’s are offsets from the center meant to be the outside corners of the plot. I believe they are correct!

Hmm and you’re not receiving any errors or anything when the script initially runs? What I would do is just print all the min/max values and the player position each 2 seconds just and see what’s going on there.

1 Like

yea I don’t get errors. I don’t see a reason to try that second part as the variables are predefined.

Well if you’re seeing incorrect behavior, it seems like it’d have to be something with the values of the variables. Your logic looks correct so either the player x,z position isn’t correctly reflecting where you’re character actually is in-game or the min/max values aren’t reflecting what you’re seeing in-game.

Unless you’ve already confirmed that’s not the case, it doesn’t hurt to try.

1 Like

It is outputting -20, 20, -20, 20. I am guessing that means that the script isn’t correctly defining the plot center. Though if I check the variable on the client and server it shows a valid location other than 0,0,0 for the plot center.

I found the fix thank you @Uhsleep91. The problem was it was setting the variable values before the player’s variables were loading.

1 Like

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