Having a Problem Tweening a model using mouse ray cast

I’m having some difficulty trying to fix the model from going to random places.
My best guess for the reason of the problem is the way I tween the CFrame for the Primarypart of the model.

Script for the Client:

local RepilcatedStorage = game:GetService("ReplicatedStorage")
local ShowCon = RepilcatedStorage:WaitForChild("ShowControl")
local Player = game.Players.LocalPlayer
local UserInputSer = game:GetService("UserInputService")
local PlacePart = RepilcatedStorage:WaitForChild("ShowTarget")
local character = Player.Character
local Mouse = Player:GetMouse()
local CanMove

if not character or not character.Parent then
    character = Player.CharacterAdded:wait()
end

ShowCon.OnClientEvent:Connect(function(IsOn)
	print(IsOn)
	if IsOn then
		CanMove = true
	else
		CanMove = false
	end
end)


function TweenEf(Tar)
	local TweenSer = game:GetService("TweenService")
	local Part = Tar
	
	local Tween = TweenSer:Create(
		Part,
		TweenInfo.new(1,Enum.EasingStyle.Sine,Enum.EasingDirection.In),
		{Color = Color3.fromRGB(225,0,0),
		 Orientation = Vector3.new(0, 50, 90)	
		}
	)
	
	Tween:Play()
	Tween.Completed:Wait()
	wait(7)
	
	local Dis = TweenSer:Create(
		Part,
		TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.In),
		{Transparency = 1}
	)
	
	Dis:Play()
	Dis.Completed:Wait()
	Part:Destroy()
end

local showTarget

Mouse.Button1Down:Connect(function(Cur)
	if CanMove then
		local mouseRay = Mouse.UnitRay
		local CastRay = Ray.new(mouseRay.Origin, mouseRay.Direction * 1000)
		local IngoreList = {character,game.Workspace.Balloon}
		local hit, position = workspace:FindPartOnRayWithIgnoreList(CastRay, IngoreList)
		if hit then
			local ExploVec = Vector3.new(position.X,position.Y,position.Z)
			local CFramePos = CFrame.new(position.X,position.Y,position.Z)
			local FinalCF = CFramePos
			print(CFramePos)
			local NewPart = game.ReplicatedStorage.Target:Clone()
			NewPart.Position = Vector3.new(FinalCF.X,FinalCF.Y,FinalCF.Z)
			NewPart.Parent = workspace
			showTarget = PlacePart:InvokeServer(FinalCF,ExploVec)
			TweenEf(NewPart)	
		end	
	end
end)

Script for the Model:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlacePart = ReplicatedStorage:WaitForChild("ShowTarget")
local TweenSer = game:GetService("TweenService")
local Ballon = workspace:FindFirstChild("Balloon")
local Primary = Ballon.PrimaryPart

PlacePart.OnServerInvoke = function(player,MouseCFrame,ExploVec)
	local BalloonPos = Primary.Position
	local ModMouse = Vector3.new(ExploVec.X,BalloonPos.Y,ExploVec.Z)
	local Reg = Vector3.new(ExploVec.X,0,ExploVec.Z)
	local mag = (BalloonPos - ModMouse).Magnitude
	local Time = mag/18
	
	TweenSer:Create(
		Primary,
		TweenInfo.new(Time,Enum.EasingStyle.Sine,Enum.EasingDirection.In),
		{CFrame = Primary.CFrame * CFrame.new(Reg)}
	):Play()
end

Any advices will be appreciated.

Can someone at least tell me how to tween a model properly based on the position. I’ve been looking around the Dev forum about this, but with no result.

You can’t move a model via TweenService directly, but you can tween a CFrameValue object and apply those changes to the model.

local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Value = model:GetPrimaryPartCFrame()

CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
     model:SetPrimaryPartCFrame(CFrameValue.Value)
end)

local tween = TweenService:Create(CFrameValue, TweenInfo.new(), {Value = TargetCFrame})
tween:Play()

tween.Completed:Wait()
CFrameValue:Destroy()
2 Likes

Now it works properly, thank you so much.