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)
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?
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.
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.
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.
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.
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.