How do I tween a model using mouse.hit.Position and math.clamp?

So I have a placement system that I am trying to make, and I want to make it move models.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local block = workspace.Block
local TweenService = game:GetService("TweenService")

mouse.Move:Connect(function()
    if mouse.Target == workspace.Part then
        local goal = {}
        local Y_OFFSET = 1
		local PLACEMENT_OFFSET = Vector3.new(((workspace.Part.Size.X % 2) + 1) * 0.5, 0, ((workspace.Part.Size.Z % 2) + 1) * 0.5)
        
        local newPosition = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z) - workspace.Part.Position
        newPosition = workspace.Part.Position + Vector3.new(math.floor(newPosition.X), Y_OFFSET, math.floor(newPosition.Z)) + PLACEMENT_OFFSET

		goal.Position = Vector3.new(
			math.clamp(
				newPosition.X,
				workspace.Part.Position.X - (workspace.Part.Size.X / 2) + 0.5,
				workspace.Part.Position.X + (workspace.Part.Size.X / 2) - 0.5),
			newPosition.Y,
			math.clamp(
				newPosition.Z,
				workspace.Part.Position.Z - (workspace.Part.Size.Z / 2) + 0.5,
				workspace.Part.Position.Z + (workspace.Part.Size.X / 2) - 0.5)
		)
	    local tweenInfo = TweenInfo.new(.1)
		goal.CFrame = CFrame.new(mouse.hit.Position)
        local tween = TweenService:Create(block.PrimaryPart, tweenInfo, goal)
        tween:Play()
    end
end)

I tried to weld the parts and use primarypart, but it didn’t work. Is there a way to keep all the elements of my current code and move the model?

1 Like

You need to create a Vector3Value locally and Tween that. Then use a .Changed function on said value to update the primary part CFrame using :SetPrimaryPartCFrame()

How would I do that. Would I do something like goal.Vector3? Thats the part where I get stuck at.

You would Tween the Vector3’s value

local tinfo = TweenInfo.new(.1)

local goal = {}
goal.Value = Vector3.new()-- make this whatever

local tween = ts:Create(vector3value,tinfo,goal)
tween:Play()

Thanks a lot, I will try it soon!

So I tried it, and it didn’t work:

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local block = workspace.Block
local TweenService = game:GetService("TweenService")

mouse.Move:Connect(function()
    if mouse.Target == workspace.Part then
        local goal = {}
        local Y_OFFSET = 1
		local PLACEMENT_OFFSET = Vector3.new(((workspace.Part.Size.X % 2) + 1) * 0.5, 0, ((workspace.Part.Size.Z % 2) + 1) * 0.5)
        
        local newPosition = Vector3.new(mouse.hit.Position.X, 0, mouse.hit.Position.Z) - workspace.Part.Position
        newPosition = workspace.Part.Position + Vector3.new(math.floor(newPosition.X), Y_OFFSET, math.floor(newPosition.Z)) + PLACEMENT_OFFSET
		goal.Value = Vector3.new(block.PrimaryPart.Position)-- make this whatever
		
		goal.Position = Vector3.new(
			math.clamp(
				newPosition.X,
				workspace.Part.Position.X - (workspace.Part.Size.X / 2) + 0.5,
				workspace.Part.Position.X + (workspace.Part.Size.X / 2) - 0.5),
			newPosition.Y,
			math.clamp(
				newPosition.Z,
				workspace.Part.Position.Z - (workspace.Part.Size.Z / 2) + 0.5,
				workspace.Part.Position.Z + (workspace.Part.Size.X / 2) - 0.5)
		)
	    local tweenInfo = TweenInfo.new(.1)
		goal.CFrame = CFrame.new(mouse.hit.Position)
        local tween = TweenService:Create(block.PrimaryPart, tweenInfo, goal)
        tween:Play()
    end
end)

I probably did something wrong.

You’re tweening the primary part. That is not what I said to do

I wouldn’t, this is a terrible way to go about it. This goes against OP’s use case in the first place.


OP, @CaptainD_veloper, what specific problem is occurring in the main post? You mentioned that it didn’t work but you didn’t give any other information. Information is required. How far does your code go until it stops working? Which part is experiencing error? Any console logs?

Vagueness is an antipattern to finding a solution to a problem.

The problem that is occuring is that it moves the primary part only, not the rest of the model. No errors or console logs.

If you’re using the PrimaryPart model tween technique, I have that outlined in a thread above (the text “I wouldn’t” leads to that in my above post). If you follow that, you shouldn’t experience any of this. To sum it up very quickly:

  • PrimaryPart
  • All parts need to be welded to PrimaryPart
  • Unanchor all parts except PrimaryPart
  • Tween PrimaryPart

Try by CFrame over Position as well, if that produces no luck.

2 Likes

You can try using Lerp. I mostly use it to tween objects on my placement system. You can set a primary part to the model and move it by

YourModel:SetPrimaryPartCFrame()

I tried this, but it didn’t work.

What of this did you try? Did you try the entire solution or only just a part of it? I can try making a repro if your problem still isn’t solved to see if I can achieve the same issue you’re having.

I got the solution, I did @xuefei123 thing and it worked.

That’s a little unfortunate. That method of tweening is pretty awful. To highlight the segment discussing this practice on the thread I posted:

Are you still interested in potentially making a weld-based solution work or are you fine with what you’ve got? Will need to know if it’s worth following through with a repro.

I would prefer the weld one yea.