How can i move a part only in the Y axis

Hello developers

I’m making a cooking system, and i need to move a part to the position of another part hitted by a raycast.

i will show you the important part of the code:

Mouse.Button1Up:Connect(function()
	
	Clicking = false
	
	if Point then
		
		raycastDownward(Point)
		
		local Highest = nil
		
		-- Check highest part
		
		local Results = raycastDownward(Point)
		
		for _,TheRay in pairs(Results) do
			if not Highest then
				Highest = TheRay
				continue
			end
			
			if TheRay.Instance.Position.Y > Highest.Instance.Position.Y then
				Highest = TheRay
			end
		end

		
		local TSinfo = TweenInfo.new(0.2)
		
		local TsAnim = TweenService:Create(Point,TSinfo,{Position = Highest.Position}) -- Important line
		TsAnim:Play()

		
		--Point.Position = Result.Position

	end

Look at the tween line, i need to move the part to the Highest part found in the rays, the tween does it right, but it moves the part in the three axis, i tried to make Highest.Position.Y inside the tween, but it gives me an error:

So how can i achieve this?

1 Like

Just use the Y axis value from the Position, don’t use the entire Position. BasePart.CFrame

You are sending the Tween to the Position, not the Position.Y value.
local TsAnim = TweenService:Create(Point,TSinfo,{Position = Highest.Position}) -- Important line

Adding to what @Scottifly has said -

local part = script.Parent

while task.wait(3) do
	part.Position += Vector3.new(0,50,0)
end

Can`t you simply do this?
This would move the part 50 studs on the y-axis consistently [because of the loop]

I can’t do that, i don’t know why, but it gives me an error while doing that.

image

Also, i can’t use CFrame because Raycasts don’t return CFrames they return Positions

I can’t use Vector3.new() for this i think, because i’m using raycasts to do this job, what the system does is that, i fire Raycasts from the part, when the raycasts hits, the CheckHighestPart section, runs a loop in a array to check which one of the hitted parts with the raycasts, is the higher one in the Y axis, so i can move the ingredient to that Y position

This is an OLD video of this, because now i’m using more than one raycast

1 Like

Mind sharing the definition of the ‘raycastDownward’ function? Since its results are what is causing the error.

This is the entire code:


--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CurrentCamera = workspace.CurrentCamera

local RS = game:GetService("ReplicatedStorage")

--//Controls
local Point = nil
local Clicking = false
local tweenInfo = TweenInfo.new(0.1)

--//Initialization
if game.Loaded then
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	CurrentCamera.CFrame = workspace.CamaraMostrador.CFrame
end

--//Functions

function raycastDownward(part)
	local results = {};
	local raycastParams = RaycastParams.new();
	raycastParams.FilterDescendantsInstances = {part};
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist;

	for _, attachment in next, part:GetChildren() do
		if attachment:IsA("Attachment") and attachment.Name == "RaycastAttachment" then
			local origin = attachment.WorldPosition;
			local direction = Vector3.new(0, -2, 0);
			
			local MidPoint = origin + direction/2
			
			results[attachment] = workspace:Raycast(origin, direction, raycastParams);
			
			
			local Part = Instance.new("Part")
			Part.Parent = game.Workspace
			Part.Anchored = true
			Part.CanCollide = false
			Part.CanTouch = false
			Part.CanQuery = false
			Part.BrickColor = BrickColor.new("Really red")
			Part.Transparency = 0.6
			Part.Size = Vector3.new(0.1,0.1,direction.Magnitude)
			Part.CFrame = CFrame.lookAt(MidPoint,origin)
			
		end
	end

	return results;
end




Mouse.Button1Up:Connect(function()
	
	Clicking = false
	
	if Point then
		
		raycastDownward(Point)
		
		local Highest = nil
		
		-- Check highest part
		
		local Results = raycastDownward(Point)
		
		for _,TheRay in pairs(Results) do
			if not Highest then
				Highest = TheRay
				continue
			end
			
			if TheRay.Instance.Position.Y > Highest.Instance.Position.Y then
				Highest = TheRay
			end
		end

		
		local TSinfo = TweenInfo.new(0.2)
		
		local TsAnim = TweenService:Create(Point,TSinfo,{Position = Highest.Position})
		TsAnim:Play()

		
		--Point.Position = Result.Position

	end
	
	
	Point = nil
	Mouse.TargetFilter = nil
	
end)

Mouse.Button1Down:Connect(function()
	if Mouse.Target and not Mouse.Target.Locked and Mouse.Target:IsA("BasePart") and Mouse.Target:FindFirstChild("Value") then
		Point = Mouse.Target
		Mouse.TargetFilter = Point
		Clicking = true
		
		RS.Sounds.GrabSound:Play()
		
		local TweenAnimation = TweenService:Create(Point, TweenInfo.new(0.2), {Position = Point.Position + Vector3.new(0, Point.Size.Y + 0.5, 0)})
		TweenAnimation:Play()
	end
end)

Mouse.Move:Connect(function()
	if Clicking == true and Point then
		local TweenAnimation = TweenService:Create(Point, tweenInfo, {Position = Mouse.Hit.Position + Vector3.new(0, Point.Size.Y + 0.5, 0)})
		TweenAnimation:Play()

		--[[
		Point.Position = Vector3.new(PosX,PosY,PosZ)
		]]
	 end
end)


1 Like

Yes, a Raycast measures Position(X,Y,Z) so you should be able to take that Y value and Tween the item just that Y value from its current Position.

1 Like

Yea, but how do i change the script to make that without the errors?

Take the ‘Highest’ items original Position, and Tween it to the same X and Z Position with the value of the raycast’s Y Position.