How to detect when player steps off part with raycasting

Hello!
I have been trying to make a landmine that when you step on it, it doesnt explode. However, when you go off of it, it does.

I know how to raycast, (to detect when a player touches the part),

local originExplode = top.Position
local direction = top.Position + Vector3.new(0,0.1,0)

But I have no idea how to detect when the player gets OFF the mine, I have tried stuff like

if not raycast then ...

However, this just makes it so the mine explodes right when you go on it.

Any help is appreciated.

2 Likes

You can use RaycastResult | Roblox Creator Documentation to find out information from the Part.
I don’t think you need to know the top of the Part though.

Basically run a function that fires when the raycast result changes. If the result Part.Name is a mine and you walk onto it you then you can use an if statement to see if is changing from that Part to any other part or Terrain. Then Kaboom!!

3 Likes

Seems like you just need to increase the length of the raycast. If you have it 0.1 studs tall, it’ll probably deactivate immediately after activating because of walking animations moving legs away from the mine, causing the raycast to not detect anything. With a longer raycast (say, 5 studs), the entire character will be detectable by the mine, which will reduce the chances of detected body parts falsely moving away from the raycast due to animations.

2 Likes

Oh yeah, sorry, I was testing around, i just left the last test I did in. I did 0.5, 1, 2, 3.

2 Likes

Oh, the “top” isnt the top surface of the part. its just what i named the part.

2 Likes

I will try this in maybe a few hours. I dont know, though. (But i certainly will try it)

2 Likes

You would need an active state for your land mines. User your RaycastResult to determine what instance is currently beneath your feat (Raycastresult.Instance) and if that instance is one of your land mines then set the land mines active state to true, once that same land mine is no longer detected, set the active state to false.

Use a :Changed() function to detect a change in this state, within that function check if this state has been changed to false, if it does, explode. (This will not trigger if the initial state is false).

2 Likes

Wouldn’t just checking for if the Changed state goes from Top (the mine’s name) to anything else work more easily?

Function Raycastresult Part.Name state Changed
if the name state changes to Top then
repeat until name state Changed()
task.wait(.2 or however long you want to wait after the player leaves the mine)
end
Kaboom!
end

2 Likes

Yeah, anything that represents change can be used in this case, I figured a state would be the easiest but use whatever you’re most comfortable with!

2 Likes

Alright. Ive looked at all of your things. I am coding it right now to see if it works.

1 Like

I am a bit confused here, where am i going to store the name of the instance the ray hits? If i use a variable, i dont think i can use :Changed() right?

1 Like

If you want the mine to explode when the player steps off of it just use

part.TouchEnded:Connect(function(Make landmine explode)
1 Like

I said with raycasting. I must use raycasting

1 Like

Isn’t that over complicating it?

1 Like

Uh, maybe, but like… i REALLY need to use raycasting.

1 Like

You could, but Touched and TouchEnded are pretty inconsistent so it may not work that well.

2 Likes

But Raycastresult returns the actual Part, right?
If you check if Raycastresult Part.Name changed to Top that sets the repeat until loop starting, which will then repeat until the Raycastresult’s Part.Name changes to anything else, then it’ll explode.

1 Like

Yo. I tried this. However, it wasnt working, so i did a test.

while not stepped do
	task.wait()
	local raycast = workspace:Raycast(originExplode, direction, rayParams)
	if raycast then
		print(raycast.Instance.Name
	end
end

To make sure the name would stay the same while standing still. This is what happened.


As you can see, while im standing still, the ray scans Left leg and Right leg, while I am standing still. This is probably due to the animations, which will make it not practical to use.

(sorry for blank screen at start)

1 Like

you need to set up parameters for your raycast and blacklist your own character since you don’t want the raycast to see yourself.

2 Likes

…But i need the raycast to hit the player… thats why im making a landmine

1 Like