How do I detect if a player is below a certain distance in vector3.Y without looping

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to detect if a player has gone below -200 in the Y axis/vector3.Y without looping

  2. What is the issue? Include screenshots / videos if possible!
    No issues yet I just want optimization

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    It is easy to think about while waits and heartbeats though I don’t want those I want a more optimize solution maybe event based though I don’t know how I would go about it

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--print("Thanks for feedback")

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Use raycasting. Whenever you want to determine a player’s location, raycasting is almost always the best choice.

Raycasting - Roblox Documentation

nope

character.HumanoidRootPart.Position.Y < -200

thats what I use though I have to loop it ykw ill just set an interval of 1 each loop so it doesnt lag asf

Checking the position wont lag. You can run that every frame and it wont cause a noticeable increase in lag

I meant memory mb aadadadadadadadaa

It wont noticeably affect memory either

1 Like

I made this server script here:

local Players = game:GetService("Players")

local function Check(plr : Player)
	
	while task.wait() do
		
		local Character = plr.Character or plr.CharacterAdded:Wait()
		
		if Character then
			
			local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
			
			if HumanoidRootPart then
				
				local y = HumanoidRootPart.Position.Y
				
				if y < -200 then
					
					print(plr.DisplayName .. " is under -200")
					
				end
				
			end
			
		end
		
	end
	
end

local function PlayerAdded(plr : Player)
	
	local Character = plr.Character or plr.CharacterAdded:Wait()
	
	if not Character then
		
		return warn("Character not found for " .. plr.Name)
		
	end

	task.spawn(function()
		
		Check(plr)
		
	end)
	
end

Players.PlayerAdded:Connect(PlayerAdded)

Oh thanks tahts a bit excessive heh ill use it though thanks

Took me only 5 minutes or something but here u go :slight_smile:

1 Like

also task.wait() waits 1 frame so u don’t have to worry about lags because it only fires 60 times per second while true tries to fire infinity times in a second

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