Trying to move a mesh with code

RayCasting would be a better but more difficult way.

Try this:

local function plant()
	local clonedDirt = placingDirt:Clone()
	local humRootPart = tool.Parent.HumanoidRootPart
	local rightLeg = humRootPart.Parent:FindFirstChild("Right Leg")

	local targetX = humRootPart.Position.X
	local targetY = (rightLeg.Position.Y - (rightLeg.Size.Y / 2))
	local targetZ = humRootPart.Position.Z
	
	clonedDirt.Position = Vector3.new(targetX, targetY, targetZ)
	print(player.Name .. " farmed dirt.")
end
1 Like

u added “text” that why it didn’t work, do it without those “”

1 Like

Oh nvm I already tried that earlier

1 Like

Did you try my code? I raycast to the floor and check if it hit terrain and if raycastResult.Material is grass.

1 Like

Will it work on parts?

more chars

1 Like


this is what my code looks like and it works as seen in output

1 Like

This line:

if raycastResult.Instance == workspace.Terrain and raycastResult.Material == Enum.Material.Grass then
2 Likes

@NateDV10 does this work?

Also, do you want it so they can only place dirt on grass?

yes, previously we used your code to figure out the positions already, he was having trouble earlier since he forgot Vector3.new() which wasn’t in your reply earlier.

No, it says attempt to index nil with 'Position'

I am lost, so the position didn’t work earlier and you are already moving on to checking for grass?!

this line was what I did:

clone.Position = Vector3.new(hrp.Position.X,hrp.Position.Y-2.195,hrp.Position.Z)

since you said you already know it will only be 2 studs down.

Here’s a full version of his script (i missed a bit)

local tool = script.Parent

local dirtFolder = workspace -- replace with where you want to store placed dirt

local serverStorage = game:GetService("ServerStorage")
local placingDirt = serverStorage:FindFirstChild("Part") -- replace part with your dirt's name

local function plant(player, character)
	
	local clonedDirt = placingDirt:Clone()

	local humRootPart = character:FindFirstChild("HumanoidRootPart")
	local humanoid = character:FindFirstChild("Humanoid")
	local rightLeg = character:FindFirstChild("Right Leg")

	if humanoid.FloorMaterial == Enum.Material.Grass then
		
		local targetX = humRootPart.Position.X
		local targetY = (rightLeg.Position.Y - (rightLeg.Size.Y / 2))
		local targetZ = humRootPart.Position.Z

		clonedDirt.Position = Vector3.new(targetX, targetY, targetZ)
		clonedDirt.Parent = dirtFolder
		print(player.Name .. " farmed dirt.")

	else
		
		print(player.Name .. " wasn't on grass.")
		
	end
end

tool.Activated:Connect(function()
	
	local character = tool.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	plant(player, character)
	
end)

This works perfectly for me, the only reason it shouldn’t is because you’re on R15. If you are on R15, let me know so I can fix it.

1 Like

The thing by @MartimDev works, but the thing we were working on does not work. I tried to do your approach, but it still does not work for some reason.


1 Like

I’m still getting errors… The HumanoidRootPart is nil even though I passed it through to the function

If you don’t mind could you add me to a team create session of your game or send me the current state of it.

@MartimDev already solved it and uses less code so I marked it as the answer

Oh, I didn’t realize that.

Glad you figured it out!

Oh wait I’m getting bugs nevermind, no idea what caused this
image
If I get close to the dirt and click it makes this weird tilt.

Always tilts or only some times?
if always, change pivot orientation to compensate.