How to detect if a player is ahead of something?

Hi, I am currently making a system where if a player left their checkpoint behind they get a notification that the player has left a checkpoint behind. I don’t know how I can achieve this, I’ve used magnitude but it doesn’t fix my problem.

2 Likes

Can you explain a bit more? Magnitude should be perfect for this situation. If the distance between the player and the checkpoint is too big, you can send the player a notification.

But I need to check if the player is 30 studs ahead of the checkpoint.

Can you use the scritping support format:

and give us more detail about the checkpoints, their size or anything else.
As I am very confused why magnitude wouldn’t work either.

If the stages are in a straight line you could check if the players z or x position is greater stage they are on + 1.

1 Like

You could simply use a combination of .magnitude and some logic based on their Vector3 values. If going “past” the checkpoint would denote a greater x axis value, then use that, otherwise use y or z. You’d first run the code on a contant increment, and first check if the player’s x, y, or z axis (read previous sentence signifies the player is “ahead” of the checkpoint, with a larger or greater value than the checkpoint. If the previous step shows true, then check if the .magnitude between the checkpoint and the player is greater than or equal to 30 studs. Remember that magnitude is the length of a vector (scalar), devoid of direction.

Something like the following (pseudocode):

local player = -- Player here
local checkpoint = -- Checkpoint here

while true do
    local rootPart = player.Character.HumanoidRootPart

    if player.Character.Humanoid.Health > 0 then -- Checks if player is still alive
        -- Whichever axis you choose switch out for X
        if rootPart.Position.X > checkpoint.Position.X
            -- Checks if player is further than 30 studs away from the checkpoint
            if (player.Position - checkpoint.Position).magnitude >= 30 then
                -- Do whatever you want
            end
        end
    wait(1)
end
1 Like

You could probably store what direction is forward in the checkpoint, like in its CFrame, and you can take the dot product of the checkpoint’s look vector with the difference between the checkpoint’s position and the player’s position combined with the difference’s magnitude check to get your intended behavior.

local checkpoint = -- checkpoint used
local character = -- character to check

local rootPart = character.HumanoidRootPart

-- change this if your checkpoint isn't a part
local deltaPos = rootPart.Position - checkpoint.Position

-- convert to X/Z only
deltaPos *= Vector3.new(1, 0, 1)

-- also change this if not checkpoint:IsA("Part")
local forward = checkpoint.CFrame.LookVector

-- also converted to X/Z
forward = (forward * Vector3.new(1, 0, 1)).Unit

if 
    not checkpoint.Claimed -- not a real property, please change this
    and deltaPos.Unit:Dot(forward) > 0
    and deltaPos.Magnitude >= 30 
then
    warn("the character skipped a checkpoint!")
end

For context, the :Dot function returns a number saying how much one vector matches the other (as long as both are .Unit).

  • If the number is 1 then both vectors match,
  • if it’s -1 then they are facing opposite directions,
  • if it’s 0 then the vectors are perpendicular.
1 Like