Stud to "Yard" Calculation

I’m developing an American football game and I’m wondering the best method to figure out a good way to calculate where you’re at on the field based on your position.

I’m not asking for a formula or anything, just any suggestions on how to go about it with the most accurate result.

(Sorry if I’m being unclear about what I mean by yards if you’re unfamiliar with American football)

Edit for clarification: I’m looking for someone to help point me in the right direction in how I determine the player’s position relative to the field’s position. I’ve been playing around with :ToObjectSpace and :ToWorldSpace but I’m bad with math. The field size is exactly 285 studs long, and each yard is 3 studs.

1 yard = 0.9144 meters
1 stud = 0.28 meters

therefor

0.9144 / 0.28 = 3.26571428571

1 yard = 3.265 studs

1 Like

I’m more so looking for a way to tell what yard line the player is at. The way my field is set up is where every 3 studs turns out to be 1 yard.

What I’m wanting to know how I determine what yard line the player is at on a field with 285 studs based off of solely their HumanoidRootPart position. What’s especially confusing is how can I accomplish this when there are sides of the field each with a ten yard line, twenty yard line, thirty yard line, and forty yard line.

Essentially, how can I use the player’s torso position to find out if they’re standing at the five yard line or the thirty yard line and which side of the field they’re on.

(See picture below if confused)

Added my edit for clarification!

.Magnitude should be useful for you here;
You can compare things distances using it, and compare people’s locations and stuff, to points on the map;

Edit; You would need more than just that, because it counts studs and if you’re for instance a little bit more higher up it could count as more studs giving an inaccurate reading; You would need probably to set these comparators Y locations to whatever what that player’s Y location before using .Magnitude;

So like,

local newVector3 = Vector3.new(comparator.Position.X, comparator.Position.Y, HumanoidRootPart.Position.Z)
comparator.Position = newVector3
local area = (math.floor(3 / (comparator.Position + HumanoidRootPart.Position).Magnitude)) * 10
area = area - 50 -- Offset it

Attention: dummy code

1 Like

I’ll play around with your psuedo code and hopefully I’ll see if I can get anything to work. Thanks for the suggestion, I really appreciate it!

I’ve been tampering with your code and suggestion and I haven’t gotten anything to work using .Magnitude. Your psuedo code, while I understand it’s psuedo code and it isn’t meant to work entirely, hasn’t been able to work for me even when modifying it. It just prints 0 no matter where the player is.

Can I see your implementation please

1 Like
Test = function(character)	
	local HumanoidRootPart = character.HumanoidRootPart
	local comparator = game.Workspace.Field.Grass.Normal.Mine
	
	local newVector3 = Vector3.new(comparator.Position.X, comparator.Position.Y, HumanoidRootPart.Position.Z)
	comparator.Position = newVector3
	local area = (math.floor(3 / (comparator.Position + HumanoidRootPart.Position).Magnitude)) * 10
	area = area - 50 -- Offset it
	
	print(area)
end

spawn(function()
	while true do
		wait(1)
		
		for index, value in pairs(game.Players:GetPlayers()) do
			if value and value.Character then
				Test(value.Character)
			end
		end				
	end
end)

I removed my edits and just implemented your psuedo code which is printing -50 constantly no matter the movement of the character.

What is Field.Normal.Mine? Can I see what it is?

1 Like

Oh I just realised I’m dumb, it’s not 3 /, it’s 1 * I guess

1 Like

Sorry I didn’t make that distinct/clear in my previous post. The “Mine” reference is a regular part that covers the exact dimensions:

Position: Vector3.new(139.241, 93.775, 110.091)
Size: Vector3.new(138.463, 9.218, 285)

I’ll try to substitute 1 * with 3 / and see if I can get it to work.

I’m kind of sleepy rn since “tonorrow” is Sunday I could probably have some better examples with actual code about 2PM Brasilia time

(sorry something popped up will not be able to)

1 Like

Haha alright. I’ll keep playing with the code and I’m sure I can figure it out. I’ll post the solution when I eventually find it. Goodnight!!

1 Like

What you can do is constantly cast a ray from the player’s root part downwards with all the yard lines as the whitelist, this should give you what yard line the player is at.

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
rayParams.FilterDescendantsInstances = workspace.YardLinesFolder

while true do
    local result = workspace:Raycast(humanoidRootPart.Position, -humanoidRootPart.CFrame.UpVector * 40, rayParams)
    if (result) then
        print("Player is at " .. result.Instance.Name)
    end
    wait(.2)
end
3 Likes