How to detect if the player went away from an object?

Hey there, I have been working on a Flee the Facility inspired game for a few days now and got stuck at my generator repairing script. The generator repairing works similarly to Flee the Facility’s computer hacking system.

What would be the best way to detect, if the player went away from the generator?

Any help is appreciated!

script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
	if script.Parent.Percent.Value == 100 then
	else
		local timeLeft = math.random(1,10)
		script.Parent.hitbox.Interact.Enabled = false
		script.Parent.Status.Value = "WORKING"

		repeat
			wait(1)
			
			timeLeft = timeLeft - 1
			if script.Parent.Percent.Value >= 100 then
				script.Parent.Percent.Value = 100
				break
			else
				script.Parent.Percent.Value = script.Parent.Percent.Value + 2
			end
		until timeLeft == 0

There are many ways to do that, you could make a loop and check if player is too far from the part by doing this:

local MagnitudeInStuds = (WhateverYouAreRepairing.PrimaryPart.Position - Player.Character.HumanoidRootPart.Position).Magnitude

now if MagnitudeInStuds is more than 25 studs for example then player is far from the generator.

You could also use :GetPartsInPart() to see if the player is standing in the hitbox or not.

(apologies for the previous reply I accidentally did that)

2 Likes

I advise to use magnitude to find distance between two positions (pos1 - pos2).Magnitude

local function GetCharFromPlr(plr)
	for i,v in ipairs(workspace:GetChildren()) do
		if game:GetService("Players"):GetPlayerFromCharacter(v) == plr then
			return v
		end
	end
	return nil
end
local HowFarCanPlayerGoInStuds = 5
script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
	local char = GetCharFromPlr(plr)
	local humrp = char:FindFirstChild("HumanoidRootPart")
	if script.Parent.Percent.Value == 100 then 
		
	elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds then -- character must be atleast 5 studs away from hitbox
		local timeLeft = math.random(1,10)
		script.Parent.hitbox.Interact.Enabled = false
		script.Parent.Status.Value = "WORKING"

		repeat
			wait(1)
			
			timeLeft = timeLeft - 1
			if script.Parent.Percent.Value >= 100 then
				script.Parent.Percent.Value = 100
				break
			elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds then
				script.Parent.Percent.Value = script.Parent.Percent.Value + 2
			else
				
				-- do here something in case your character leaved that place
				break
			end
		until timeLeft == 0
	end
end)
2 Likes

Also you can check if player’s humanoid health is greater than zero.

local function GetCharFromPlr(plr)
	for i,v in ipairs(workspace:GetChildren()) do
		if game:GetService("Players"):GetPlayerFromCharacter(v) == plr then
			return v
		end
	end
	return nil
end
local HowFarCanPlayerGoInStuds = 5
script.Parent.hitbox.Interact.Triggered:Connect(function(plr)
	local char = GetCharFromPlr(plr)
	local humrp = char:FindFirstChild("HumanoidRootPart")
	local hum = char:WaitForChild("Humanoid")
	if script.Parent.Percent.Value == 100 then 
		
	elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds and hum.Health > 0 then -- character must be atleast 5 studs away from hitbox
		local timeLeft = math.random(1,10)
		script.Parent.hitbox.Interact.Enabled = false
		script.Parent.Status.Value = "WORKING"

		repeat
			wait(1)
			
			timeLeft = timeLeft - 1
			if script.Parent.Percent.Value >= 100 then
				script.Parent.Percent.Value = 100
				break
			elseif (humrp.Position - script.Parent.hitbox.Position).Magnitude < HowFarCanPlayerGoInStuds and hum.Health > 0 then
				script.Parent.Percent.Value = script.Parent.Percent.Value + 2
			else
				
				-- do here something in case your character leaved that place
				break
			end
		until timeLeft == 0
	end
end)

Thank you so much! I really appreciate it.

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