Trying to move a mesh with code

Hi, I am trying to move a mesh with my code for my farming system. I have a piece of dirt in ServerStorage that gets cloned (the tilled land that you plant crops on). I am trying to move this to the players HumanoidRootPart position.

The problem with just changing the position is that if I am on a hill and I do something like this:

local function plant()
	local clone = placingDirt
	local hrp = tool.Parent.HumanoidRootPart
	
	clone.Parent = game.Workspace.Spawn.Farming
	
	clone.Position = (hrp.Position.X, 0, hrp.Position.Z) --Not that this would work just an example
end

This results in the tilled land going inside the ground at Y: 0.

7 Likes

Maybe raycast from the sky to see how high the ground is first before you decide the Y must be 0?

5 Likes

something like this I think

local veryhighnumber = 100000
local verylownumber = -100000
local rayOrigin = vector3.new(hrp.Position.X, veryhighnumber, hrp.Position.Z)
local rayDestination = vector3.new(hrp.Position.X, verylownumber, hrp.Position.Z)
local rayDirection = rayDestination - rayOrigin
local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
if raycastResult then
    clone.Position = raycastResult.Position
    clone.Orientation = raycastResult.Normal -- imagine assuming the ground is always flat, couldn't be us
end

I never tested my script, tell me if it works.
also consider adding

if raycastResult.Material == "Grass" then
end
4 Likes

What would very high and low number do? Could I make the high one math.huge?? Sorry, I am very new to raycasts.

2 Likes

Uh like any number you want really, I am just subtracting the vector 3 to get a direction because for some reason, in the past if I just do like vector3.new(0,-1,0) or something for ray direction, it doesn’t work for me.

I am assuming math.huge won’t work but you can try, like I said, I haven’t tested my code, and could not right now.

2 Likes

It seems like the subtraction would give you (0, -200,000, 0) because you are subtracting the x and the y with the x and the y so they cancel out. Also -100,000 - 100,000 would equal -200,000. Should I just do:

rayDirection = (0,0,0)
1 Like

Just try my code first breh…

you can try that, but make sure you do vector3.new, and also make sure the y value is not zero since the subtracting find the distance between the vector from how I understand it…

I am also new to scripting :stuck_out_tongue:

2 Likes

Where would I put this code? In the script for my hoe to make it activate?

2 Likes

Inside your function??? like idk, I thought we were talking about how we will change your function?!?

Just place in function and see what happens lol

2 Likes

That’s exactly what I was talking about lol

2 Likes

That’s going to be a problem, because raycasts have a maximum distance of 5000 studs. Also, you should always make your raycasts as short as you can, since it limits the worst-case performance by not unnecessarily checking for collisions across your entire world…

Orientation is set from a Vector3 of Euler angles. The result.Normal vector is a unit vector that is the direction of the surface normal, which is not angles, just a direction. So this line of code would (if the ground is flat) give your part a 1-radian rotation about the Y-axis, which is not going to be what you expected.

Depending on how simple this game is, you may be able to avoid the raycast entirely by getting the Y position from one of the following:

  • Directly from whatever part is already at the player’s feet (e.g. a baseplate)
  • Just placing the item below the hrp.Position.Y by (0.5 * hrp.Size.Y + Humanoid.HipHeight + 0.5 * dirt.Size.Y) (if character is standing)
  • If the ground height is the same everywhere, you could just hardcode it too.
4 Likes

It’s long, but this should work.

local leg = tool.Parent:FindFirstChild("Right Leg")

clone.Position = (hrp.Position.X, leg.Position.Y - (leg.Size.Y / 2), hrp.Position.Z)
3 Likes

Could I use the HumanoidRootPart?

2 Likes

Yes just add the leg variable if you want it to be shorter. Also, I realized I could shorten the code even more, so check the edited one.

2 Likes

Isn’t the y taken from the middle of the part?

2 Likes

I suppose you could calculate the y by adding half the size.Y of the part

unless you changed pivot or something

2 Likes

Yes. I didn’t mean you’d be able to simply copy Position.Y, I meant that you may be able to calculate the position you need from a part that you know is part of the ground, rather than having to do a raycast. Like if you had a baseplate. You still need to account for the thickness of the parts.

2 Likes

yea raycast was a bad idea since in Humanoid.FloorMaterial you can literally avoid the problem of trying to till while jumping and the part appear in the air…

I got blinded by raycast since I recently was doing raycast lol

2 Likes

I get a error when I use this: Assigning 3 values to 1 variable leaves some values unused.

2 Likes

Ok so now I have the the distance from the players HumanoidRootPart to the ground (2.195), and a way not till the ground while jumping (thanks @phenix_rider1), but how do I actually teleport the mesh?

My current code:

local function plant()
	local clone = placingDirt
	local hrp = tool.Parent.HumanoidRootPart
	local hum = hrp.Parent.Humanoid
	
	clone.Parent = game.Workspace.Spawn.Farming
	
	if hum.FloorMaterial == "Grass" then
		--teleport stuff here
	end
end
3 Likes