How to detect when a player walk out of a path?

how can i detect when a player walks out of a Humanoid:MoveTo thing?

i got a script and i created a part and when the player click on the part the player goes there with Humanoid:MoveTo and a gui is enabled but if the player walks out the gui is still visible
how can i detect when the player walk out from it and then disable the gui?

local Gifts = workspace:WaitForChild("Gifts")

for i, thing in pairs(Gifts:GetChildren()) do
	thing.ClickDetector.MouseClick:Connect(function(player)

		local char = player.Character or player.CharacterAdded:wait()

		if (char:WaitForChild("HumanoidRootPart").Position - thing.Position).magnitude < 50 then

			for _,gift in pairs(Gifts:GetChildren()) do
				gift.Circle.SurfaceGui.Enabled = false
			end
			thing.Circle.SurfaceGui.Enabled = true
			local humanoid = char:WaitForChild("Humanoid")
			local RootPart = char:WaitForChild("HumanoidRootPart")
			
			humanoid:MoveTo(thing.Position)
		end
	end)
end

if someone can help me out and tell me a way to do that thanks :slight_smile:

You could put walls on the edge of whatever you want, and make it so that it runs code when touched.

(probably not the best way to do things)

I suggest looking into humanoid:MoveToFinished(), I can’t say for certain as I am not too familiar with it but it could solve your issue.

As @1AIIHailThanos, MoveToFinished is what you need as it returns a parameter that tells you if the MoveTo was completed or it was timed out, I think what you could do is disable the GUI if it’s false? The official article for it says it’ll return false if it Timed out, so I don’t know if it would work if the player moves during the MoveTo

local Gifts = workspace:WaitForChild("Gifts")

for i, thing in pairs(Gifts:GetChildren()) do
	thing.ClickDetector.MouseClick:Connect(function(player)

		local char = player.Character or player.CharacterAdded:wait()

		if (char:WaitForChild("HumanoidRootPart").Position - thing.Position).magnitude < 50 then

			for _,gift in pairs(Gifts:GetChildren()) do
				gift.Circle.SurfaceGui.Enabled = false
			end
			thing.Circle.SurfaceGui.Enabled = true
			local humanoid = char:WaitForChild("Humanoid")
			local RootPart = char:WaitForChild("HumanoidRootPart")
			
			humanoid:MoveTo(thing.Position)
			
			humanoid.MoveToFinished:Connect(function(completed)
				if not completed then
					--Disable Gui
				end
			end)
		end
	end)
end

i tried using it, it didnt work i tried also to print in the function and also didnt work :confused:

Is it required that they are able to move during the MoveTo or do you prefer if the Movement keys are disabled temporarily till the MoveTo is done?

yea they need to be able to move

Then I’m not sure what could be done, It’s quite odd that it doesn’t detect if it was cancelled in MoveToFinished, maybe you can try checking if the WalkToPoint was changed? MoveTo sets this to the point to move the HUmanoid to, so maybe you can use a GetPropertyChangedSignal event for the WalkToPoint of the humanoid so if changed, you can do something.

If you need me to explain more, I can

something like that does not work

                humanoid:GetPropertyChangedSignal("WalkToPoint"):Connect(function()
                    if humanoid.WalkToPoint ~= thing.Position then
                           --printing something
                    end
                end)

its not working, is it what you meant for me to do?

Did you add a print to see if it’s actually working?

yes i did try to print but it does not work if im walking but if im clicking on another part like that it prints like 6 times

That’s…Odd I think when you move with the movement keys, it doesn’t update the WalkToPoint, I’m honestly stumped, maybe in moveto, also specify the part?

humanoid:MoveTo(thing.Position,thing)

And instead of using GetPropertyChangedSignal on WalkToPoint, use it on WalkToPart?

nope this thing isnt working ://///////////////////////////////////////

Okay I’m incredibly stumped, there’s no information about it on the DevForums either

i know its possible i just dont know how to do that :confused:

There has to be a way to do, but neither me or you can see it, we probably have to wait for someone to come who may have the solution. I know we can’t really detect key inputs since this isa Server Script and UserInputService only works in Localscripts

this is a local script, but still it wont work for mobile if i do that…

Actually we can make it work for both PC and Mobile.

For PC users, you can do it simply by checking if they’re pressing WASD and disable the GUI

For mobile, you can try to use UserInputService’s TouchMoved event so when they move their screen, remove the gui. You may have to do a few checks to see if the position where they moved is close to where they would move their player

well i can try that, but i will still wait for today until the smart guy with the answer will come :slight_smile: thanks for helping btw

1 Like

Anytime, I wish you good luck to getting a proper, non-botched answer, if you unfortunately get nothing after a while, I hope you can somehow make my answer work almost 100%. If you have any other issues, I can try to do my best to help